hacker hacker - 5 months ago 23
Objective-C Question

accessing the UInavigationcontroller embedded view controller?

I had an iPhone application(with storyboard) in which i am using a view-controller as initialviewcontroller.Then after that i added a view-controller scene and embedded a navigation-controller to it.I want that navigation-controller to be the rootviewcontroller of the window.When i am trying to access that view controller programatically and push another viewcontroller to it the navigation controller is not there,i also getting the warning of scene unreachable on that navigation controller embedded to that view controller.I am trying to achieve it like this

`
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
bundle: nil];

UINavigationController *controller =(UINavigationController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Newcontroller"];
NSLog(@"%@",[controller topViewController]);

[appdelegate.window setRootViewController:controller];
`


can anybody help me how to access this navigation controller programatically ?

Answer

you need to do like

Step-1

Create the custom class of NavigationController , for example NavigationViewController (SubClass of UINavigationController).

Step-2

Assign the class name for that NavigationController in Identity Inspector.

Step-3

Set the Storyboard ID for that class in Identity Inspector Xcode.

Step-4

Finally access the class via Code, for example

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle: nil];

NavigationViewController *controller =(NavigationViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"navigate"];
NSLog(@"%@",[controller topViewController]);

[appdelegate.window setRootViewController:controller];

for example

enter image description here