UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
AllListsViewController *controller = navigationController.viewControllers[0];
controller.dataModel = _dataModel;
This is my former code. I can access the AllListsViewController through navigationController since it's the rootViewController.
But now my storyboard changed. I've tried to access the controller the same way, but it wouldn't work.
SWRevealViewController *revealViewController = (SWRevealViewController *)self.window.rootViewController;
UINavigationController *navigationController = (UINavigationController *)revealViewController.frontViewController;
AllListsViewController *controller = navigationController.viewControllers[0];
controller.dataModel = _dataModel;
Maybe it's because I set the frontViewController in storyboard so this revealViewController.frontViewController is nil. But I don't know other ways to access it from AppDelegate.m.
Sorry I can't post any images because of my reputation. Here's my code.https://github.com/liukaiyi54/CheckList
Somebody help me please. I've been tortured by this for days.
Thanks in advance!
Try this:
//This is Main unless you have named your storyboard something different
UIStoryboard *storyBoard =[ UIStoryboard storyboardWithName:#"Main" bundle:nil];
//make sure to put this in "AllListsViewController" in your identifier in the storyboard
UINavigationController *vc = [storyBoard instantiateViewControllerWithIdentifier:#"AllListsViewController"];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = myNavigationController;
This is where you would put your storyboard ID in the blue box:
Related
I am trying to inject a reference to a property in a uitableviewcontroller from my appdelegate. The first is successful - I am able to reference xmppStream from my uitableviewcontroller, but the second appears to destroy the reference somewhere and I'm not sure why.
First - ok, xmppStream in uitableviewcontroller is not nil;
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
PhoneNumberTableViewController *myViewController = (PhoneNumberTableViewController *)navigationController.topViewController;
[myViewController setXmppStream:[self xmppStream]];
Second - gets a reference but is destroyed i.e. in uitableviewcontroller xmppStream is nil?
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
PhoneNumberTableViewController *tv = [sb instantiateViewControllerWithIdentifier:#"UserRegistration"];
[tv setXmppStream:[self xmppStream]];
Any help would be greatly appreciated.
Thanks
what should try is make a property of PhoneNumberTableViewController and whenever you need to use is use that variable. like
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
_myViewController = (PhoneNumberTableViewController *)navigationController.topViewController;
[myViewController setXmppStream:[self xmppStream]];
I have a custom UIPageViewController which contain 5 UIViewControllers, now I would like to navigate from one of those 5 UIViewController, But I can't because in those UIViewControllers have no UINavigationController. Can anyone suggest me , how I can navigate from one those 5 UIViewControllers to other UIViewController?
The code in your - [AppDelegate application:didFinishLaunchingWithOptions:]
might look like this (after checking your skipping condition of course):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"MyAuth"]; //if you assigned this ID is storyboard
UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"Login"]; //if you assigned this ID is storyboard
UIViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:#"Details"];
NSArray *controllers = #[vc1, vc2, vc3];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController setViewControllers:controllers];
If you paste just this to - [AppDelegate application:didFinishLaunchingWithOptions:], you'll see that it works immediately.
In my AppDelegates 'didFinishLaunchingWithOptions' function, I have this code in there:
if(loggedIn != nil)
{
MainViewController *mvc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.window setRootViewController:mvc];
}
Second Attempt which didn't work:
if(loggedIn != nil)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainView"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
}
The problem is that when the MainViewController loads, the NavigationHeader is missing. I've tried various methods online and instantiations that basically do the same thing to no avail. I have also tried created a whole new navigationController and adding my view to it, however, that fails as well.
Your setting MainViewController as your root, if this is not a navigation controller, there will be no header when it opens.
Instead create a UINavigationController, set MainViewController as its root and then set the navigation controller as the window root.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"home"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
or if you have the navigation controller inside the storyboard then instantiate that. Most likely the initial view controller.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
[self.window setRootViewController::[storyboard instantiateInitialViewController]];
not sure if it will be the initial viewController or not, that requires more info of your setup to know.
I want to add a navigation view controller prior to the user getting to the splitview controller. I have tried a few ways of changing the root controller when I want to go from navigation controller to splitview controller but I don't seem to be setting the delegate the right way when I do this.
Code WITHOUT nav view (works perfectly):
AppDelegate
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
Code with nav view prior to SplitView
AppDelegate
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"dummy"];
self.window.rootViewController = rootController;
[self.window makeKeyAndVisible];
DummyViewController
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
This takes me from the DummyViewController that I launched into, to the splitview controller which is the initial view controller in Storyboard. Which is fine however, when I do it this way none of the delegates get called. This is probably because when changing root controllers, it is not setting the delegates properly. How can I get this to work the right way?
It seems the only really non-hacking way to do it is to present a modal view over the split view in the detail view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DummyViewController *dummy = (DummyViewController *)[storyboard instantiateViewControllerWithIdentifier:#"dummy"];
[self presentViewController:dummy animated:NO completion:nil];
By setting animation to NO, the user does not see the split view loaded behind it.
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;