Here is some code:
ViewControllerA.h
@interface ViewControllerA: UIViewController {
NSString *myString;
}
@property (nonatomic,retain)NSString *myString;
ViewControllerA.m
@implementation ViewControllerA
@synthesize myString;
Then i push ViewControllerB in the navigationcontroller. When i want to return to ViewControllerA
ViewControllerB.m
-(IBAction)randomAction(id)sender {
ViewControllerA *myViewController = (ViewControllerA*)[self parentViewController]:
[myViewController setMyString:@"test"]: // HERE IS THE ERROR : "[DMINavigationController setMyString:]:unrecognized selector sent to instance"
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES]
}
Why? Is it normal that error reffers to “[DMINavigationController setMyString:]” instead of my viewcontroller?
I think you are a bit confused about ViewControllers and how to handle them properly in a NavigationController.
You probably started with an
DMINavigationControllerand initialized it with anViewControllerA. If you push a new ViewControllerViewControllerB, the parent won’t beViewControllerAbut rather the NavigationController.If you want to access the previous ViewController you need to query the
viewControllersproperty of your NavigationController to find it.The VC you are looking for is at (index of current Object)-1