I have an app with an "old" Tabbarcontroller and a MainWindow.xib. I have to delete the tabbarcontroller logic to transform the app and made a "Left side menu" type, like facebook. I have a problem, i have alredy created the left side menĂ¹ with a tableviewcontroller and i can open,close and push correctly my controllers... Now i want to insert a different customnavigation bar class each navigation controller (in the old version of the app each navigation controller had his custom navigation class to change che image in relation with the active viewcontroller). This is the code i use in the tableview didselect method of a row in my left side menu:
if (indexPath.row==1) {
DemoViewController *demoController = [[DemoViewController alloc] init];
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
[navigationController setValue:[[CustomNavigationBar alloc]init] forKeyPath:#"navigationBar"];
NSArray *controllers = [NSArray arrayWithObject:demoController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
}
this code manage correctly the slide and load correctly the viewcontroller inside MFSideMenu. The custom navigationbar class is assigned but not works correctly:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
this method is never called, maybe MFSideMenu overwrites the uinavigationcontroller delegate? How i can do to made "active" again this navigation controller delegate method?
found a solution,this code:
DemoViewController *demoController = [[DemoViewController alloc] init];
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
CustomNavigationBar *navClass=[[CustomNavigationBar alloc]init];
[navigationController setValue:navClass forKeyPath:#"navigationBar"];
[navigationController setDelegate:navClass];
NSArray *controllers = [NSArray arrayWithObject:demoController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
instead of:
DemoViewController *demoController = [[DemoViewController alloc] init];
UINavigationController *navigationController = self.menuContainerViewController.centerViewController;
[navigationController setValue:[[CustomNavigationBar alloc]init] forKeyPath:#"navigationBar"];
NSArray *controllers = [NSArray arrayWithObject:demoController];
navigationController.viewControllers = controllers;
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
In this way the delegate is assigned correctly...
Related
when we will navigate to another view controller. then using back button popviewcontroller property we can come to first view controller.
but what to do when we using MFSideMenu drawer. while clicking on drawer menu navigate to another screen. then how to come back on first view controller.
for navigating to another viewcontroller. I have used following code.
SCMainMenuViewController *scMainView = [[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"scMainMenuController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scMainView];
[self.menuContainerViewController setCenterViewController:nav];
[self.menuContainerViewController setMenuState:MFSideMenuStateClosed completion:^{
}];
Try this:
HomeView *Home = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
NSArray *controllers = [NSArray arrayWithObject:Home];
self.navigationController.viewControllers = controllers;
I have a view controller say 'HomeViewController' on which I have added a UITabBarController, which has 4 UITabBarItems. Now what I want is, when I select the HomeViewController no tab should should be selected for the first time and another view should be loaded. I have tried UITabBar instead of UITabBarController but its not working. I have also tried :
tabBarController.selectedViewController=nil; //giving crash
tabBarController.selectedIndex=-1; //not working either
UITabBarItems should be selected when user will click on them.
You have to add UITabBar object on HomeViewController.
In ViewDidLoad you have to add following code:
ViewController1 *controller1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
ViewController2 *controller2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
viewControllersArray = [[NSArray alloc] initWithObjects:controller1,controller2, nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [[NSArray alloc] initWithArray:viewControllersArray];
for(int i = 0; i < tabBarController.tabBar.items.count; i++){
[[tabBarController.tabBar.items objectAtIndex:i] setTitle:[[tabBar.items objectAtIndex:i] title]];
}
isItemSelected = NO;
Also set the tag of tab bar items starting from 0. As 0 for first item, 1 for second item and so on.
and implement delegate method:
(void)tabBar:(UITabBar *)tabBar1 didSelectItem:(UITabBarItem *)item{
if(!isItemSelected){
isItemSelected = YES;
AppDelegate *appDel = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDel.window.rootViewController = tabBarController;
}
tabBarController.selectedIndex = item.tag;
tabBarController.selectedViewController = [viewControllersArray objectAtIndex:item.tag];
}
I think this is not the right way, as Concept of creating TabBar says that " Show the particular controller of related tab". If u do not select any tab than what else you will show on screen, "A black screen ?". so please re-architecture your design.
I am developing a ViewController (login app) with a single button, when I press this button I want to appear my UISplitView like this:
- (IBAction)loadSplitViewController:(id)sender {
[self showSplitViewController];
}
and the code developed for the creation of my splitViewController is this:
-(void)showSplitViewController{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle: nil];
LeftViewController *leftViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftViewController"];
RightViewController *rightViewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"RightViewController"];
UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftViewController];
UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightViewController];
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:leftNavController, rightNavController, nil];
leftViewController.delegate = rightViewController;
splitViewController.delegate = rightViewController;
[self presentViewController:splitViewController animated:YES completion:^{}];
}
the thing is... if I use for display my splitViewController this line:
[self presentViewController:splitViewController animated:YES completion:^{}];
throws me an error
I also tried with
[self.view addSubview:splitViewController.view];
but this way my splitViewController never rotates, and delegates doesn't work as well... and I don't want my splitViewController to be a subview of my viewController, I want it to appear more like an independient modalView
any help I'll appreciate
thanks in advance
Split view controllers really should be the root view controller of the window (in fact Apple says it has to be, though there seem to be some examples where this isn't true). Instead of presenting it, you could just switch the window's root view controller to be the split view controller.
self.view.window.rootViewController = splitViewController;
I want to create UISplitView programmatically after pressing a button in a view. For UISplitView, I have two view controllers called as MyTableViewController and MyDetailViewController. This is what I am doing in the action of the button:
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
MyTableViewController* firstVC = [[MyTableViewController alloc] init];
MyDetailViewController* secondVC = [[MyDetailViewController alloc] init];
UINavigationController *leftNavController = [[UINavigationController alloc] init];
[leftNavController pushViewController:firstVC animated:NO];
splitVC.viewControllers = [NSArray arrayWithObjects:leftNavController, secondVC, nil];
AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitVC];
But secondVC (the detail view) doesn't show up. It is just black. Though, the firstVC (the table view) shows up as it should be. What could be the reason?
You should take a look at using [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewIdentifier"], and set an identifier for the view controller in your storyboard. Calling init on a view controller doesn't reference the storyboard you have created, so you need to instead pull the view controller out of the storyboard. The table view worked because its layout is programmed into the UITableViewController class.
I created a new xcode project as View-based application, and I have a set of UIViewController(s) which I plan to use inside separate UINavigationController(s).
In ParentViewController.m before all the UINavigationController(s) and after all myViewControllers been initiated:
NSMutableArray *navControllers = [[NSMutableArray array];
for (id aVC in self.myViewControllers) {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];
//[aVC setNavigationController:navController];
[navController setNavigationBarHidden:YES];
[navController setToolbarHidden:YES];
[navControllers addObject:navController];
[navController release];
}
_navigationControllers = [[NSArray arrayWithArray:navigationControllers] retain];
_navigationControllers is retained as a member of ParentViewController, so I suppose all my navigation controllers initiated inside for-loop are kept by _navigationControllers so they won't be released or become nil, but when I try to use navigationController in MyViewController to push SomeOtherViewController, it doesn't work:
- (IBAction)pushDetailView {
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
I put a breakpoint before pushViewController:someOtherViewController, and "po [self navigationController]", the console tells me it is a nil reference.
I assumed that when I do
[[UINavigationController alloc] initWithRootViewController:aVC], the underlying mechanism would assign the navigationController as aVC.navigationController, because the Apple "View Controller Programming Guide for iOS" does the same without assigning navigationController to rootController.
Unless I unmark the second line of the for-loop //[aVC setNavigationController:navController];, the navigationController does not exist in aVC.
Am I misunderstanding the mechanism? Is there another solution for my case?
Thanks in advance!
_navigationControllers = [NSMutableArray array];
for (id aVC in self.myViewControllers) {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aVC];
//[aVC setNavigationController:navController];
[navController setNavigationBarHidden:YES];
[navController setToolbarHidden:YES];
[navControllers addObject:navController];
}
// assuming index 0 navigation controller is with 'ParentViewController'
self.rootViewController = [_navigationControllers objectAtIndex:0];
check with this.