I have a problem trying to access a navigation controller of the view controller from it, always returns as nill to me, though it is shown within the navigation controller.
Here is what I have (I have a split view controller, that is presented as tab controller for master and viewcontroller (inside navigation controller) as detail):
FirstDetailViewController *fdvc = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailViewController" bundle:nil];
UINavigationController *fdvcNav = [[UINavigationController alloc] initWithRootViewController:fdvc];
NSArray *ipadVCs = [[NSArray alloc] initWithObjects:tabController, fdvcNav, nil];
UISplitViewController *splitvc = [[UISplitViewController alloc] initWithNibName:nil bundle:nil];
[splitvc setViewControllers:ipadVCs];
[[splitvc view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"splitViewControllerBG"]]];
[splitvc setDelegate:fdvc];
[[self window] setRootViewController:splitvc];
[[self window] makeKeyAndVisible];
But when I'm trying to access a navigation controller from the fdvc view controller in ViewDidLoad with [self navigationController] it gives me (Null) always.
Thanks!
I fixed it. Turned out, that I had to move my code from ViewDidLoad method to ViewDidAppear and it worked fine.
viewDidLoad is getting called before the navigationController property has been updated, that was my mistake.
Related
I get a EXC_BAD_ACCESS error message in my AppDelegate.m program when I attempt to goback to the previous view controller by popping the current view controller off the stack. The error obviously means that the viewcontroller is not in the stack.
My code for initializing my first view in my AppDelegate.m program is as follows:
CEMMainViewController *mc = [[CEMMainViewController alloc]
init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mc];
self.window.rootViewController = navController;
When I want to bring up a new view controller I do the following:
CEMUpdateBurialViewController *oc = [[CEMUpdateBurialViewController alloc] init];
[self.navigationController pushViewController:oc animated:YES];
When I want to return to the previous view I do the following which causes the EXEC_BAD_ACCESS error. So why isn't the previous view in the stack? I just need to know what I am doing wrong.
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:YES];
Check your view controller hierarchy with the following code:
NSLog(#"%#",self.navigationController.viewControllers);
And try this method:
[self.navigationController popToRootViewControllerAnimated:YES];
I had an application in which I need to have a login or register system before the tab bar controller is added to the window, such as in Instagram.
I have added 5 navigation controllers (with view controller as its root) to the UITabBarController and then set it as the root of the window. Before that, I need to have another UINavigationController for the login system.
If I add that, how do I remove it before adding tab?
Another problem is that I also have to handle logging out, so I need to come back to it.
Can anybody help me with me how to do this?
I suggest to you loading Loginview From Delegate window as we did Normally. and from Logged Success its button click you set TabbarController like this:-
UIViewController *viewControllerPostalCode2 = [[cntrServices alloc] initWithNibName:#"cntrServices" bundle:nil];
UIViewController *viewControllerPostalCode3 = [[cntrInquiryViewController alloc] initWithNibName:#"cntrInquiryViewController" bundle:nil];
UINavigationController *navPostage1 = [[UINavigationController alloc] initWithRootViewController:viewControllerPostalCode2];
UINavigationController *navPostage2 = [[UINavigationController alloc] initWithRootViewController:viewControllerPostalCode3];
//
navPostage1.navigationBar.tintColor =DARK_BACKGROUNDNavigation;
navPostage2.navigationBar.tintColor =DARK_BACKGROUNDNavigation;
//
self.tabBarForServicesController = [[UITabBarController alloc] init];
self.tabBarForServicesController.delegate=self;
self.tabBarForServicesController.viewControllers = [NSArray arrayWithObjects:navPostage1,navPostage2,nil];
[self.navigationController pushViewController:self.tabBarForServicesController animated:YES];
I Done this type of Task using this method and at the Logged Out just poptoRootviewController work back to the Logged in Screen.
I have done this like adding my tabbarcontroller as rootViewController to UIWindow and in applicationbecomeactive delegate i present a controller with navigation controller like when required and simply dismiss the controller when authentication done
UIViewController *topViewController = [self.navController topViewController];
if (![topViewController isKindOfClass:[LGLoginViewController class]]) {
[self.navController popToRootViewControllerAnimated:YES];
self.navController = nil;
LGLoginViewController* loginView = [[LGLoginViewController alloc] initWithNibName:#"LGLoginViewController"bundle:nil];
if (!self.navController) {
self.navController = [[UINavigationController alloc] initWithRootViewController:loginView];
} else {
[self.navController initWithRootViewController:loginView];
}
self.navController.delegate = self;
[self.window.rootViewController presentModalViewController:self.navController animated:NO];
}
I'm trying to convert my app to universal for iPad support and whatever I do I can't get rid of the "Splitview controller is expected to have a view controller at index 0 before it's used!" error right after the app stars.
I'm using iOS6 as target, XCode 4.6.3. Tried all the things that I could find on this website and Google, didn't help me at all.
I want to add a TabBar controller as a Main one (left one in the Split Controller) and some other controllers as a detail one.
Here is my current code in AppDelegate.m:
tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:[NSArray arrayWithObjects:navAddVC, nav, svcNav, stvcNav, nil]];
FirstDetailViewController *fdvc = [[FirstDetailViewController alloc];
initWithNibName:#"FirstDetailViewController" bundle:nil];
UINavigationController *fdvcNav = [[UINavigationController alloc] initWithRootViewController:fdvc];
viewControllers = [[NSArray alloc] initWithObjects:tabController, fdvcNav, nil];
UISplitViewController *splitvc = [[UISplitViewController alloc] initWithNibName:nil bundle:nil];
[[splitvc view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"splitViewControllerBG"]]];
[splitvc setViewControllers:viewControllers];
[splitvc setDelegate:fdvc];
[[self window] setRootViewController:splitvc];
[[self window] makeKeyAndVisible];
I'd appreciate any help, thanks.
Thanks to #Wain I solved it.
The thing was that by setting up a background image to my split vc I was loading its view.
So the solution is to set viewControllers array before the setting background color.
I am creating a UINavigationBar using the following,
UINavigationController *navigationController = [[UINavigationController alloc] init];
[view addSubview: navigationController.view];
How come it shows up like this?
(source: gyazo.com)
I want it to be on the top of the view but there is space there, why?
Thanks.
You are not creating things correctly. You should create your view controller, then create a navigation controller, passing the view controller as the nav controller's root controller. Then you make the nav controller the app's root controller.
UIViewController *vc = ... // create your view controller
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
window.rootViewController = nc;
The code you posted does not create a nav bar, it creates an entire navigation controller.
Put this code to ViewWillAppear
- (void)viewWillAppear {
UINavigationController *navigationController = [[UINavigationController alloc] init];
[view addSubview: navigationController.view];
}
Also you should resize nav controller to self controller bouns
i know how to add uinavigationcontroller as root view and also how to push view in uinavigationcontroller.
What i am confused in the first view which is a root view is a simple view with button when this button is tapped it will show second view.
I want this second view as uinavigationview.
any ideas how to do it, i tried to search for the solution but didn't get any nor any similar question is asked before.
basically i am trying to work without interface builder to learn things in depth.
assuming you are presenting the 2nd view controller modally
instead of
UIViewController *vc = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:vc animated:YES];
do:
UIViewController *vc = [[[UIViewController alloc] init] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
[self presentModalViewController:nc animated:YES];