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;
Related
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:
I would like to display a badge value on my tab bar everytime the below method gets called. The NSLog appears in the console, so the method works properly, but somehow the tab bar badge doesn't appears anyway. Am I doing it wrong? Or this part should be good and I missed something elsewhere?
// AppDelegate.m
- (void)pubnubClient:(PubNub *)client didReceiveMessage:(PNMessage *)message {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"tab"];
UITabBarController *tabController = (UITabBarController *)tabBarController;
[[tabController.viewControllers objectAtIndex:0] tabBarItem].badgeValue = #"1";
NSLog(#"SHOW BADGE");
}
UINavigationController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"tab"];
UITabBarController *tabController = (UITabBarController *)tabBarController;
This line (aside from the strange multiple casting) instantiates a new tab bar controller, it does not return a reference to the existing one. You should keep a reference to the existing tab bar controller instead of creating a new one.
Depending on your setup, you may be able to use the following instead:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
I have a program with a Navigation Controller and a default RootViewController. If I do nothing programmatically, the app launches and the RootViewController works as I expect, such as what the storyboard below denotes:
The issue that I have is by incorporating the Optional Start ViewController. What I want is this: In my AppDelegate (didFinishLaunchingWithOptions), I want to have code like this:
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"OptionalStartViewController"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
To first show the Optional Start ViewController. Then, after the user has finished with the Optional viewcontroller, they can then display the RootViewController.
So in the Optional Start ViewController, I've added code like this to show the Root View Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RootViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];
This all works except the RootViewController, when shown, does not have the navigation controls as expected (i.e. the view is shown without navigation controls).
I've also tried the code below (using UINavigationController instead of ViewController) with the same results...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self appDelegate].window.rootViewController = viewController;
[[self appDelegate].window makeKeyAndVisible];
One other twist... there could be several optional start view controllers.
Any ideas?
Delete the UINavigationController
select the controller ("optional start view controller" in our case)
click Editor >> Embed In >> Navigation Controller
Now select the Navigation Controller and Right Side Utility area, Select the option Is Initial View Controller
If you want to change the root view controller dynamically then its better to do it programmatically, in didFinishLaunchingWithOptions, Get the window instance , initialize navigation controller with the root view controller and then set windows root view controller to navigation controller.
Here is the code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
OptionalFirstViewController *optionalFirstViewController = [[OptionalFirstViewController alloc] initWithNibName:#"OptionalFirstViewController" bundle:nil];
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:optionalFirstViewController];
[optionalFirstViewController release];
self.window.rootViewController = homeNavigationController;
[homeNavigationController release];
[self.window makeKeyAndVisible];
return YES;
}
and make sure that you uncheck Is Initial View Controller for all view controllers in story board
Hope this helps
Without using storyboard we can set programmatically in AppDelegate Class, just write down these lines of code in AppDelegate.
AppDelegate.h
#property (strong, nonatomic) UIStoryboard *storyboard;
#property (strong, nonatomic) UINavigationController *navigationController;
AppDelegate.m
if(self.window == nil)
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
if(self.navigationController == nil)
self.navigationController = [[UINavigationController alloc]init];
if(self.storyboard == nil)
self.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.navigationController.navigationBarHidden = YES;
// Here we can check user is login or not also,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:ivc animated:YES];
self.window.rootViewController = ivc;
[self.window setRootViewController:self.navigationController];
[self.window makeKeyAndVisible];
In Swift Version
var window: UIWindow?
var storyBoard :UIStoryboard?
var navigationController : UINavigationController?
var mainController :MainViewController = MainViewController()
if window == nil {
window = UIWindow(frame: UIScreen.main.bounds)
}
if navigationController == nil {
navigationController = UINavigationController()
}
if storyBoard == nil {
storyBoard = UIStoryboard(name: "Main", bundle:nil)
}
navigationController?.setNavigationBarHidden(true, animated: true)
// storyboard with identifer
mainController = storyBoard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
navigationController?.pushViewController(mainController , animated: true)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
You can't set a new root view controller to UINavigationController after it is initialised. You should modify viewControllers array of UINavigationController according to your needs.
If you want the optional VC to be root VC, create #[optionalVC] and set it as viewControllers array of UINavigationController.
In the AppDelegate i check if there is a user existing, if there is i "skip" two ViewControllers by pushing a NavigationController using this code:
if(currentUser){
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Test"];
self.window.rootViewController = viewController;
viewController.navigationController.navigationBarHidden = NO; // Tried this nothing happened.
[self.window makeKeyAndVisible];
}
else{
[PFUser enableAutomaticUser];
[[PFUser currentUser] incrementKey:#"RunCount"];
[[PFUser currentUser] saveInBackground];
[[PFUser currentUser] fetch];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"firstView"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
So the true statement skips two ViewControllers and push the UINavigationController. But when it shows it this way there is no top bar.
However if there was no user then it would proceed to the UINavigationController in a "normal" way by segues then the bar is visible.
How can i fix this?
Thank you!
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
UIViewController *viewController = [storyboard
instantiateViewControllerWithIdentifier:#"Test"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = nav;
"So the true statement skips two ViewControllers and push the UINavigationController." No, it doesn't. First of all you're not pushing anything, you're setting the window's root view controller. Secondly, you haven't created any navigation controller, you've only created viewController.
If viewController has a navigation controller in the storyboard, then you should instantiate the navigation controller (which will, in turn, instantiate viewController), and make it the window's root view controller.
I think you should set a UINavigationController to rootviewController , there will be a navigation bar. So you can also set the firstView as rootViewController, but set the viewcontrollers attribute to put the Test viewcontroller that clears before viewcontrollers.
I am currently using ViewDeck with Storyboards, and have the following setup in the application didFinishLaunchingWithOptions:
//View Deck Setup
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController* menuController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftSideMenu"];
UINavigationController* navigationController = (UINavigationController *) self.window.rootViewController;
self.viewDeckController = [[IIViewDeckController alloc] initWithCenterViewController:navigationController leftViewController:menuController rightViewController:nil];
self.window.rootViewController = self.viewDeckController;
However, when I am setting a new CenterController from my MenuViewController, the navigation bar is removed, even if loading the same center view controller as I was previously looking at.
- (IBAction)viewUsers:(UIButton *)sender
{
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController* viewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"middleViewController"];
[self.viewDeckController setCenterController:viewController];
[self.viewDeckController closeLeftView];
}
What am I doing incorrectly?
The solution is remove any "deck constructor" class between AppDelegate and you TabBar. When you need to set or change Deck structure I create a several methods for each one structure. For example, if you want Deck has "leftView" view controller in left side, "rightView" in the right and "tabBarHome" in center, create a method like this in App Delegate:
-(void) lanzarHome{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
IIViewDeckController* deckController =[[IIViewDeckController alloc] initWithCenterViewController:[storyboard instantiateViewControllerWithIdentifier:#"tabBarHome"]
leftViewController:[storyboard instantiateViewControllerWithIdentifier:#"leftView"]
rightViewController:[storyboard instantiateViewControllerWithIdentifier:#"rightView"]];
deckController.leftSize = 100;
self.window.rootViewController = deckController;
[self.window makeKeyAndVisible];
}
Now, you must call this method from your viewController with sentence like this:
[((VKAppDelegate*) [[UIApplication sharedApplication] delegate]) lanzarHome];
The most important think is that create the method that changes or inits Deck structure from AppDelegate and never you have another class or viewController between delegate and center TabBar.
I hope this answer solve your issue. For me was a very hard work to find out this issue.