0

I've a View Controller named as MasterViewController having a tableView implemented in it (as a part of my module). It contains following methods:

- (void)viewDidLoad
- (void)didReceiveMemoryWarning
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Now I want to integrate my Module in My Main Project named as "myProject" having its view controller ViewController.

How Can I init my MasterViewController in ViewController i.e call its init method. My current approach is only loading (void)viewDidLoad method and showing empty table view in my ViewController keeping in view that my MasterViewController have an interface builder of storyboard named as Main

My Approach:

 MasterViewController* mainVC = [[MasterViewController alloc] init]; 
            [self.view insertSubview:mainVC.view belowSubview:tabBar]; 
developer
  • 668
  • 1
  • 6
  • 24
  • Easy solution if using storyboard: in storyboard viewController scene you add. Inside its view, add viewcontroller widget and assign MasterViewController as a class. IBOutlet this MasterViewController widget to ViewController.h file. – Mrunal Dec 17 '14 at 16:20
  • You have to use UIView containment to add the subview, that is the preferred way to do it and you can do it in storyboard or in code. – Sandeep Dec 17 '14 at 16:21
  • @Mrunal I'm not getting exactly, how to add "its view". Please explain me in more detail. – developer Dec 17 '14 at 16:24
  • Like you add UIButton in a scene, add a UIViewController to your class ViewController's scene. – Mrunal Dec 17 '14 at 16:28
  • @k6sandeep How can I use containment, please answer me in more detail. – developer Dec 17 '14 at 16:28
  • @Mrunal I'm currently doing this, I've a tabbar, and calling method `didSelectedItem` and `item.tag` in switch operation to get the button clicked. After geting, I want to init that. – developer Dec 17 '14 at 16:31

1 Answers1

0

What you can do is in viewDidLoad of ViewController class add the following;

In Viewcontroll.h:

- (void)viewDidLoad
{
 ..... 

    MasterViewController *MSVC = (MasterViewController * )[self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
   [self addChildViewController:MSVC];
   [self.view addSubview:MSVC.view];

.....
}

Note that you have to call both addChildViewController and addSubview in order for your child view to display. You should not init your MasterViewController in your ViewController's init method.

Ohmy
  • 2,201
  • 21
  • 24
  • Giving error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' – developer Dec 17 '14 at 19:36
  • Do you also need to call didMoveToParentViewController: after adding thr child ? – Sandeep Dec 19 '14 at 07:03
  • user3166680: That is the problem in your array object. Not the view controller's. for K6z: that's is the delegate method you don't need to call in this case. Unless you want to add further implementation when you view controller is added. See below thread for the delegate. http://stackoverflow.com/questions/12909788/what-exactly-willmovetoparentviewcontroller-and-didmovetoparentviewcontroller – Ohmy Dec 19 '14 at 19:00