UINavigationController madness? - ios

I have two issues that I don't understand and am hoping that someone can help.
This code doesn't work for taking my existing UINavigationController hierarchy from a split view controller and taking over the screen with it. I just get a dark screen
UINavigationController* myself = self.navigationController;
[myself removeFromParentViewController];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"QuestionnaireViewController"];
[myself pushViewController:controller animated:YES];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.window.rootViewController = myself;
However, this code does work. Why can I set the rootViewController to a new UINavigationController but not self.navigationController?
UINavigationController *navController = [[UINavigationController alloc] init];
UINavigationController* myself = self.navigationController;
[myself removeFromParentViewController];
navController.viewControllers = myself.viewControllers;
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"QuestionnaireViewController"];
[navController pushViewController:controller animated:YES];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.window.rootViewController = navController;
My second issue is in restoring the navigation controller to the splitViewController when the user goes "back". I know from experience that I can assign a new UINavigationController to the detailView, but I cannot assign self.navigationController.
I think the issues are the same issue. For some reason a new UINavigationController is not the same as a UIView's navigationController. Why?

Meddling with UIWindow is not very safe.
Depending on what you are trying to achieve, I can think of 2 rather simple options
a) iOS 5.1+ has an option to show/hide the RootViewController of your split and show with a swipe gesture, and work with that
b) Create your own UIViewController that emulates UISplitViewController, and hide the left part whenever you need to

Related

MMDrawerViewController objective-c push to navigation view controller

i am beginner iOS dev, I have my app with many controllers.
After login on LoginViewController (LoginViewController has segue relation with NavigationViewController) I do init MMDrawerViewController with this code:
-(void)initSidebarController{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *leftView = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftViewController"];
UIViewController *centerView = [mainStoryboard instantiateViewControllerWithIdentifier:#"CenterViewController"];
UINavigationController *leftNav= [[UINavigationController alloc]initWithRootViewController:leftView];
UINavigationController *centerNav= [[UINavigationController alloc]initWithRootViewController:centerView];
appDelegate.drawerController= [[MMDrawerController alloc]initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
appDelegate.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
appDelegate.drawerController.closeDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
appDelegate.window.rootViewController = appDelegate.drawerController;
[appDelegate.window makeKeyAndVisible];
}
Now I want to push DishesViewController on NavigationViewController when user didSelectRowAtIndexPath .
How can I do this correctly ?
Thanks
Go to the storyboard, select dishesViewController and select the identity inspector from the left pane and in the StoryboardID enter "dishesVC" then use this code :
CateringDishesViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"dishesVC"];
[self.navigationController pushViewController: animated:YES]
Please follow below step, I am sure it will work for you.
1) Get top ViewController (Reference link).
2) If your MMDrawerController library configure is proper then you will get MMDrawerController as a top view controller.
3) Then find center UINavigationController
4) By using center navigation controller you can to new view controller into stack.
If you don't able to do it then provide sample source code with us. So we can provide exact solution.

present a storyboard UIViewController from a UIScrollView subview

I am trying to present a view controller from a UIScrollView subview.
I've tried using AppDelegate window.rootViewController presentViewController: but that gives me the "view is not in the hierarchy!" error.
I want to avoid using addSubview because that breaks MVC and seems to remove the controller's functionality (buttons stop working).
When I use the expected presentViewController method, I get "No visible #interface for "InititalScrollViewSubview" declares the selector "presentViewController:animated:completion:", which I think means that my initialScrollViewSubview is trying to use presentViewController but presentViewController has to come from a UIViewController. UIScrollView is without the presentViewController method.
My code is something like:
-(void)setupTouchIDButtonTapped: (id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newViewController" bundle:nil];
NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:#"myNewVC"];
//First thing I tried:
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window.rootViewController presentViewController:NewViewController animated:YES completion:nil];
//second thing I tried
[self addSubview:NewViewController.view];
//third thing I tried:
[self presentViewController:NewViewController animated:YES completion:nil];
}
The InitialScrollViewSubview has to remain the way it is. Ideally, I'd refactor everything so the InitialScrollViewSubview is another UIViewController but I work for a huge company and the app is way too large :)
Any advice is greatly appreciated!!
Thanks!!
If you are trying to 'embed' a view controller's view within your scrollview then you dont present the view controller. You instantiate it and then add it's view to the view hierarchy.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newViewController" bundle:nil];
NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:#"myNewVC"];
[scrollView addSubview:NewViewController.view];
You will also need to setup the size/frame of the view or setup autolayout constraints so that the view controller is correctly sized.
Try this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newViewController" bundle:nil];
NewViewController *myNewViewController = [storyboard instantiateViewControllerWithIdentifier:#"myNewVC"];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UINavigationController * rootNvc = delegate.window.rootViewController ;
[rootNvc pushViewController:NewViewController animated:YES];

Accessing UITabBarController from Appdelegate

I am trying to access my UiTabBarController from a UIViewController that is in UIWindow,
Since its not part on UITabBarController - using self.tabBarController.. won't work as it will be nil.
So I tried this code:
BBAppDelegate *appDelegate = (BBAppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = appDelegate.window.rootViewController.tabBarController;
When it step through the code with the debugger - I can see appDelegate does have a tabBarController and it is not nil. However on the next line
UITabBarController *tabBarController = appDelegate.window.rootViewController.tabBarController;
This results in tabBarController instance being nil and the tabBarController in the appDelegate still have a memory address assigned to it and its not nil.
What am I doing wrong here?
Edit
Here is what my debugger looks like:
Thanks!
Edit 2
This is who I setup the side menu which is added to rootViewController
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone"
bundle: nil];
BBFilterViewController *loginController=[[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"FilterViewController"]; UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginController];
self.tabBarController = [mainStoryboard instantiateViewControllerWithIdentifier: #"HomeScreen"];
MVYSideMenuOptions *options = [[MVYSideMenuOptions alloc] init];
options.contentViewScale = 1.0;
options.contentViewOpacity = 0.5;
options.shadowOpacity = 0.0;
MVYSideMenuController *sideBarController = [[MVYSideMenuController alloc]initWithMenuViewController:navController contentViewController:self.tabBarController options:options];
self.window.rootViewController = sideBarController;
[self.window.rootViewController addChildViewController:self.tabBarController];
[self.window makeKeyAndVisible];
If you have tab bar controller as root view controller you can access it like:
UITabBarController *tabBarController = (UITabBarController *)appDelegate.window.rootViewController;
//Extended
Based on update to your question you should be able to get reference to tab bar controller from last child root view controller array:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController.childViewControllers.lastObject;
From you debugger I think it should be UITabBarController *tabBarController = appDelegate.tabBarController;

How to add an overlay to a UITabbarController

Hi I have a UITabBarController which i want to add on top an overlay containing a registration form.
I've seen several places that the way to go is to use this command:
[[[UIApplication sharedApplication] keyWindow] addSubview:registrationView];
But How can I create registrationView from the storyboard and be able to access it from the UITabBarController?
Note: My registrationView should hide the tabs so i can't put it in one tab.
Hey this will solve you issue
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
ViewController *RegistrationView = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[tabBarController presentModalViewController:RegistrationView animated:YES];
[tabBarController.selectedViewController viewDidAppear:true];
do this in - (void)applicationDidBecomeActive:(UIApplication *)application function

ios 5 opening a specific viewcontroller with the correct TabBar and NavController wrapped

I will try to explain this as best as I can.
My application has a TabBarController which functions as the main navigation
I have a modal view that I segue to to add a list. that screen can be reached from 2 different viewcontrollers.
From the main route I simple just close the modal and all is fine. However from the second route I need to be able to open up an entirely new ViewController.
The issue that I am having is that I can not seem to open that ViewController with the TabBar and NavBar included.
This is the code I am currently playing with to try to get it to work.
UITabBarController *tabController = [self.storyboard instantiateViewControllerWithIdentifier:#"MainInterface"];
tabController.selectedIndex = 1;
//_window.rootViewController = tabController;
UINavigationController *groceryNavController = [self.storyboard instantiateViewControllerWithIdentifier:#"MainNavController"];
UIViewController *groceryViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"GroceryViewController"];
UIViewController *currentVC = self;
[currentVC.navigationController pushViewController:groceryViewController animated:YES];
One way to to do it is through the delegate. If in the delegate, the relevant navigation controller is called:
self.navigationController
Then you would have to do:
YourAppDelegate *delegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:groceryViewController animated:YES];
(replace "YourAppDelegate" with the actual name of your app delegate)

Resources