1

In the following setup, Button 1 calls First VC and Button 2 calls Second VC

enter image description here

Currently, I use the following code to call SecondVC by tapping Button 2.

MainVC.m

@implementation MainVC()
....

UINavigationController *navController = [self.childViewControllers objectAtIndex:0];
SecondVC *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[navController pushViewController:secondVC animated:YES];

The code above displays SecondVC just fine. However, in the First VC there are statements that need be executed before "Second VC" is presented, that are being skipped!

I execute these statements in First VC inside -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. When the Next Button is tapped in the First VC, all the statements execute because it triggers prepareForSegue before presenting SecondVC. This step is skipped when the code above is used.

Extra Info:
A) Though a Navigation Controller, Main VC in my app does not show the navigation bar in my app. I believe this would be against HIG to display two navigation bars on an iPhone. I show it here to identify Main VC.
B) There are textFields in FirstVC that User fills out. If I just copy the code, will it read and save the data from textFields.text?

Question: How can I call prepareForSegue from First VC when Button 2 is tapped in MainVC? or is there another approach to this?

user1107173
  • 10,334
  • 16
  • 72
  • 117
  • Is there anything in your design that prevents you from just copying the code from `prepareForSegue` into the method called when `Button 2` is pressed? – Kamaros Oct 22 '13 at 03:44
  • There are textFields in FirstVC that User fills out. If I just copy the code, will it read and save the data from textFields.text? Thanks. – user1107173 Oct 22 '13 at 03:46
  • No, it wouldn't. You'd need to access the data from MainVC and copy it to SecondVC. See my new answer for details. – Kamaros Oct 22 '13 at 04:02
  • Have you tried by connecting pushing the view controller Second via Xib by establish the segue action in xib?> – Ganapathy Oct 22 '13 at 04:35

1 Answers1

0

You could try something like the following:

UINavigationController *navController = [[self childViewControllers] objectAtIndex:0];
UIViewController *vc = [navController topViewController]
if ([vc isKindOfClass:[FirstVC class]]) {
    FirstVC *firstVC = (FirstVC *)vc;
    SecondVC *secondVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"second"];
    // Copy and read data from [[firstVC textField] text] to secondVC.
    [navController pushViewController:secondVC animated:YES];
}
Kamaros
  • 4,536
  • 1
  • 24
  • 39