I'm using storyboards and I've referenced the storyboard and I'm getting a value back using my Storyboard ID.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navigationController = (UINavigationController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"NavigationController"];
This is done within the UIViewController -viewDidLoad: method that's currently at the root of the UINavigationController hierarchy.
In the storyboard I've chosen to show both the UINavigationBar and UIToolbar associated with UINavigationController. I then set them to hidden in my code for the hell of it:
UINavigationBar *navigationBar = navigationController.navigationBar;
[navigationBar setHidden:YES];
UIToolbar *toolbar = navigationController.toolbar;
[toolbar setHidden:YES];
but they're still visible! Why the heck are they still visible? Stepping through the code tells me that these variables have a non-nil value. Furthermore, I would obviously get a run-time error if they were nil.
I have also tried the methods on UINavigationController:
[navigationController setNavigationBarHidden:YES];
[navigationController setToolbarHidden:YES];
These don't work either.
I would like to note that the program doesn't crash. It just glides over these methods with no warnings. Checking the state of these "bars" indicates that they are in fact hidden (using isNavigationBarHidden and isToolbarHidden on UINavigationController instance) but this isn't apparent in the simulator.
edit:
problem was instancing the navigationController and assigning it to a local variable instead of accessing it through the embed navigation controller property of a UIViewController
Problem solved
Related
I have one tabviewcontroller and associated tab bars and view controllers as shown in the image.
My problem is, when i am trying to navigate to some inner view controllers using below code it is not even working, i have tried some other code also, some codes are navigating properly but Tabbar is missing.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:#"eventList"];
self.window.rootViewController = tabbar;
[self.window.rootViewController.navigationController pushViewController:tabbar animated:YES];
i am not that much familiar with tabviewcontroller.
Change,
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:#"eventList"];
self.window.rootViewController = tabbar;
[self.window.rootViewController.navigationController pushViewController:tabbar animated:YES];
With,
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NavigationControllerOfTabbar *navVC = (NavigationControllerOfTabbar *)[storyBoard instantiateViewControllerWithIdentifier:#"navVCStoryboardID"];
self.window.rootViewController = navVC;
Problem,
You need to learn know how UITabBarController works: Read this.
You need to learn how iOS app flow/stack works, specifically for UINavigationController, UITabBarController.
In your code, this line would be nil: self.window.rootViewController.navigationController
Solution,
Create new class subclassing UINavigationController, and give first view controller's name in your storyboard.
Now initialize this with given code, and it will work.
Tab bar didn't require navigation controller to operate.
My suggestion is to remove all code give as above, and make sure that your storyboard is main interface in your target. Tabs bar should work automatically.
I've noticed that you have other tab bar controllers as children of main tab bar controller. Perhaps they are rudiments of your learning process and had to be removed.
Please make sure that you are setting up proper segues
https://imgur.com/a/WvIrh
This really should work. This is one of the simplest things to achieve in iOS and for some reason it's just not working.
I have two view controllers in my storyboard. One is InitViewController and the other is ViewController, with a storyboard ID of Init and ViewOne respectively. I have a button on InitViewController that is running code to switch the views. The code is running properly but nothing happens despite that fact. Here is the code:
-(IBAction)NextPage:(id)sender{
ViewController *wc = [[ViewController alloc]
initWithNibName:#"ViewOne"
bundle:nil];
[self.navigationController pushViewController:wc animated:YES];
}
I imported ViewController.h, I just don't know why this isn't working.
initWithNibName: is for view controllers that you create in .xib files, not storyboards. To create a view controller from a storyboard use -[UIStoryboard instantiateViewControllerWithIdentifier:]
ViewController *wc = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewOne"];
[self.navigationController pushViewController:wc animated:YES];
You need a NavigationController - try to log self.navigationcontroller and you will see is null
In your storyboard add a UINavigationController set it as entrypoint, set InitViewController as root view controller and then you can use:
ViewController *wc = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewOne"];
[self.navigationController pushViewController:wc animated:YES];
I am trying to access an Array variable in a view controller in an application that is using storyboards.
BACKGROUND:
I have been following along with the Ray Wenderlich tutorial on Storyboards.
Once I finished the tutorial, I went back tried a different route, though I’m having trouble accessing a view controller. Everything is pretty much the same except my set up is the initial Scene is a View Controller. I am to the part where the author is adding some data to NSMutableArray in his table.
THEIR CODE THAT I AM USING AS A GUIDE
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][0];
PlayersViewController *playersViewController = [navigationController viewControllers][0];
playersViewController.players = _players;
I was hoping it would be a simple as replicating what I had seen with view controllers, passing along the appropriate type, but no.
I have View Controller > View Controller > Navigation View Controller > UITableViewController.
MY CODE:
UIViewController *vc = (UIViewController*)self.window.rootViewController;
UIViewController vc1 = [vc viewController][0];
UINavigationController *nc = [vc1 viewController][0];
SearchViewTableViewController *svc = [nc viewControllers][0];
svc.myarray = _myarray;
I have tried multiple combinations and am getting nowhere.
There has got to be a simpler way for me to reference classes/view/scenes.
Any help?
Make sure you are importing the ViewController header file.
Make sure you have given you ViewController a Storyboard identifier.
Then something like this should work:
MyViewController *myVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
myVC.myMutableArray = [NSMutableArray new];
....
I've successfully implemented MFSideMenu so that all the navigation works properly except scrollsToTop in the scroll views (Specifically in my TableViewControllers and one ViewController where the UIscrollview takes up the whole frame). scrollsToTop is not working, even though it is enabled in each TableView.
I know that it's the MFSideMenu causing the issue because I removed it from the project and the scrollsToTop is working fine then.
So here's how I call the MFSideMenu from the AppDelegate on launch:
//Instantiate the Side Menu and the center view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UITabBarController *tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"MainTabBar"];
SideMenuViewController *leftSideMenuController = [[SideMenuViewController alloc] init];
UINavigationController *leftViewNavigationController = [[UINavigationController alloc] initWithRootViewController:leftSideMenuController];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tabBarController
leftMenuViewController:leftViewNavigationController
rightMenuViewController:nil];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
The TableViews I'm concerned with are embedded in NavigationControllers, which are embedded in the tabBarController referenced in the code above. And when I remove this block of code, the scrollsToTop works properly.
I think I need to adjust the container to include the TableViewControllers somehow. Any Ideas on how to do that?
The answer to my own question:
The problem appears to be that I had the left menu inside a navigation controller. So I abandoned that, and made a storyboard layout for the menu controller to use instead. And in that storyboard layout I added a navigation bar to cover up the black space that normally appears in this version of MFSideMenu.
So the new window setup in AppDelegate is...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UITabBarController *tbc = [storyboard instantiateViewControllerWithIdentifier:#"MainTabBar"];
//Then put get the left side menu controller, but inside of a navcontroller
SideMenuViewController *leftSideMenuController = [storyboard instantiateViewControllerWithIdentifier:#"SideMenu"];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:tbc
leftMenuViewController:leftSideMenuController rightMenuViewController:nil];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
The scrollview problem in the other UIViewController turned out to be an Autolayout issue.
I'm implememting a design based on the TabbedBanner example in the iAdSuite. I have a UINavigationController in the first tab. In that UINavigationController I have a view controller that simply has a button that pushes to another view controller. The pushed view controller is set to Hide Bottom Bar On Push in Interface Builder.
Here is the code where I'm setting up the UITabBarController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:#"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
_tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
_tabBarController.delegate = self;
FirstViewController *firstView = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:firstView];
_tabBarController.viewControllers = #[[[BannerViewController alloc] initWithContentViewController:firstNav], ];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Everything works except the TabBar does not get hidden when I push to the next view controller. I have tried hiding the TabBar using the Interface Builder check box as well as using nextViewController.hidesBottomBarWhenPushed = YES and neither way works.
If I remove the BannerViewController implementation, the TabBar hides exactly as it should.
It seems to me that the BannerViewController is interfering with the UINavigationController being able to hide the TabBar.
Is it possible to use Hides Bottom Bar When Pushed to hide the TabBar in this type of setup?
Thanks
Note: I realize that the code above only has one tab. I removed the other tabs for clarity.
I think this is happening because the BannerViewController itself is just a container viewController and it never actually pushes another view controller. The view controllers are pushed within the container.