Greetings everyone.
I posted a problem very much like this one a while ago which was to call a method found in V.C.1 from V.C.2.
bob133 was kind enough to help me on this. The conversation was cut short due to me moving from one country to another.
In this conversation bob133 offered me the code to do this.
- I had to import ViewController1Birth.h into ViewController2.h so I could access the - (void) hideBirthFrameOutlet2 method.
- I had to declare the hideBirthFrameOutlet2 method in the ViewController1Birth.h file to make it 'public' so it can be accessed by the (IBAction) goBackToP1 method in ViewController2.m.
- Then I went to the ViewController2.m file and keyed in the goBackToP1 method that bob gave me like so:
- (IBAction) goBackToP1:(id)sender
{
NSArray <__kindof UIViewController *> *viewControllers = self.navigationController.viewControllers; / get the current list of view controllers */
ViewController1birth *previousController = viewControllers [viewControllers.count - 2]; / Retrieve the previous view controller in the stack: remember that the last object in an array will be
at index count - 1, so I'll grab the second-to-last because the last one is the view controller on top.
Store this in a local variable because there's no need to hang on to the reference after we're done here. */
[previousController hideBirthFrameOutlet2]; / this hides the BirthFrameOutlet2 method in ViewController1 */
[previousController outsideStop];
[self.navigationController popViewControllerAnimated: YES];
}
And it worked!! What it did was when I popped the current View Controller off the stack and went back to the previous View Controller the birthFrameOutlet2 would be hidden from view.
Well, when I settled back into my work after the move I tired to do a very similar thing, by hide/unhiding an Outlet in the next View Controller rather than the previous one.
So I went through almost the same steps
- I imported ViewController2.h into ViewController1birth.h so I could access the - (void) showF13Outlet method.
- I declared the showF13Outlet method in the ViewController2.h file to make it 'public' so it can be accessed by the (IBAction) showFleaOutletOnNextPage method in the ViewController1birth.m file.
- Then I went to ViewController1birth.m file and keyed in the showFleaOutletOnNextPage method like so:
- (void) showFleaOutletsOnNextPage
{
NSArray <__kindof UIViewController *> *viewControllers = self.navigationController.viewControllers;
ViewController2 *nextViewController = viewControllers [viewControllers.count + 1];
[nextViewController showF13Outlet];
}
In this new attempt the app starts up without problems. However, when I turn to the birth page and tap the button that calls the showFleaOutletOnNextPage method, I get this in the console: "-[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 1]".
I think that the first line "NSArray <__kindof UIViewController *> *viewControllers = self.navigationController.viewControllers;" Creates an NSArray of View Controllers that have been put on the stack and because I am trying to call a method on a View Controller that hasn't been put on the Stack with the code "[viewControllers.count + 1]" I'm getting this error message.
I know this was a long winded explanation but I had to get across my problem which is, how do I call a method from one method to the next or following method.
Any suggestions?
JR