Wow, I gave a big thought on the question!
So I have a view Controller called "ContentView" within another view Controller (Main VC). The Main VC has a Navigation Controller which was created using Storyboards. And the contentView loads 3 different view controllers (vc1, vc2 and vc3) depending on the options that a Segmented Control has. So the question now is:
How can I load a new View Controller from the button within one of the subviews (vc2) that will appear once the user selects the option from the segmented control?
Although I have the visible view controller (vc2) in my storyboard, obviously I cannot connect an action to the button to the vc2' file's owner since the Nav Controller is on the Main VC.
I tried to access it with the following code:
AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
UINavigationController *navigationController = (UINavigationController*)del.window.rootViewController;
DetalleMisMarcas *detalleMarcas = [[DetalleMisMarcas alloc] initWithNibName:@"DetalleMarcas" bundle:nil];
[navigationController pushViewController:detalleMarcas animated:YES];
But it does not work.
I have tried to find a solution from this forum, but I had no luck. Most consider the existence of a Main Window Xcode 4.2 does not have.
Finally, the way I load the 3 subviews, is here:
-(IBAction)segmentCtrlChanged:(id)sender {
UISegmentedControl *seg = sender;
if (seg.selectedSegmentIndex == 0)
{
MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];
mm.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mm.view];
}
else if (seg.selectedSegmentIndex == 1) {
MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];
mp.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mp.view];
}
}
-(IBAction)mainSubView:(id)sender
{
MMViewController *mm = [[MMViewController alloc] initWithNibName:@"MTView" bundle:nil];
[contentView addSubview:theMTView];
mm.view.frame = CGRectMake(0, 0, 320, 240);
}
Any ideas?
EDIT: Based on your comment, I think delegates will work better. This is how you may implement it.
Let's say in contentView controller class, add a delegate
// in the header file
@property (weak, nonatomic) id <ContentViewControllerDelegate> delegate;
@protocol ContentViewControllerDelegate <NSObject>
- (void)contentViewDidSelectAView:(UIView *)view;
@end
// in the implementation
- (IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
case 0:
[self.delegate contentViewDidSelectAView:[[self.storyboard instantiateViewControllerWithIdentifier:@"MMView"] view];
break;
// and the rest of the code
}
// in MainView header file
@interface MainViewController : UIViewController <ContentViewControllerDelegate>
// in MainView implementation
- (void)contentViewDidSelectAView:(UIView *)view {
[self.view addSubView:view];
}
I haven't actually implemented the above code. But I think this should be the way for a controller to talk to its parent. Hope this one helps.
I am not sure what exactly you are doing but let's say you have a UINavigationController in the root, and mainViewController is linked via segue as a relationship. As you have implemented, the segmentedControl in mainVC should be linked to -(IBAction)segmentCtrlChanged:(id)sender
with valueChange
event.
In this method you can switch the index, rather than using if, although they work the same, and you can also use UISegmentedControl *
instead of id
:
-(IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
switch(sender.selectedSegmentIndex) {
case 0: {
MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];
[self.navigationController pushViewController:mm animated:YES];
break;
}
case 1: {
MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];
[self.navigationController pushViewController:mp animated:YES];
break;
}
case 2: {
// similar to above
break;
}
default:
break;
}
vc1, vc2 and vc3, -- I assume -- are instances of UIViewController. So make sure that their class is linked, and that their identifiers are set properly. and then, just leave them as they are, because you are not going to make any segue for them individually. They are going to be pushed to the stack via code.
Hope it helps.