How to change the navigationController's rootViewController - objective-c

In the appDelegate,I create a A_NavigationController to become self.window.rootViewController,and set a X_ViewController to become the rootViewController, after an other viewController pushed,I create a new B_NavigationController ,seted a new Y_ViewController to become the rootViewController and used presentModalViewController method ,added the B_NavigationeController. Now I want to do is,when I dismissed the Y_ViewController can change the A_NavigationController's rootViewController.
self.viewController = [[AViewController alloc] initWithNibName:#"AViewController" bundle:nil];
self.navCon=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = self.navCon;
BViewController *bView=[[BViewController alloc]initWithNibName:#"BViewController" bundle:nil];
UINavigationController *navCon=[[UINavigationController alloc]initWithRootViewController:bView];
[self.navigationController presentModalViewController:navCon animated:YES];
//in BViewController.m
//I want a method to change the window's rootViewController.

The "rootViewController" is only the UIViewController on the bottom of the navigationStack. To change it you can just set the object at index 0 to whatever controller you want

Related

PresentModalViewController not showing TabBar

I'm trying to go from one xib to another and I'm using TabBar.
When I move from xib to xib with PresentModalViewController I lose the TabBar.
When I use this way, it fail (like force close in android):
FirstViewController *fvc = [[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
[fvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;
UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:fvc bundle:nil];
[self presentModalViewController:tabControl animated:NO];
When I use:
UITabBarController *tabControl = [[UITabBarController alloc] initWithNibName:#"FirstViewController" bundle:nil];
I get black screen with TabBar.
Since it all fail I guess it is not the right way.
So, what is the right way to do it?
The code above crashes because you are trying to pass a view controller instead of an NSString object in the initWithNibName:bundle: method.
The way to do it depends of what you really want to do. Do you want to present the xib in a modalViewController with or without a tabBar?, or simply present the view controller modally?.
UPDATE:
Fine, you have to create first your view controllers associated to each tabBar button (like you have been doing until now), after, add these view controllers to your tabBar and then present the tabBarController modally. Like this:
FirstViewController *fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
fvc.userSelectedLatitude = saveLatitude;
fvc.userSelectedLongitude = saveLongtitude;
UITabBarController *tabControl = [[UITabBarController alloc] init];
[tabControl setViewControllers:[NSArray arrayWithObjects:fvc, nil]];
[tabControl setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:tabControl animated:NO];
I think this code should work. So, try it and tell us if something goes wrong.
With UITabBarController, there is no need to manually present viewControllers or call code to switch the views. This is handled for you.
All you need to do is set the viewControllers property of the UITabBarController. Like so:
[tabBarController setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];

Add NavigationController to current view

I'm using a UIScrollView to display some images, and i need to present or push another view when the user selects one part of the image.This ScrollView is created only programmatically.
How i can programmatically add a navigationController to it and present another view?
I tried this way but just give me the following error :
Pushing a navigation controller is not supported'
StatesViewController * controller = [[StatesViewController alloc]initWithNibName:#"StatesViewController" bundle:nil];
UINavigationController * Navcontroller = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController: Navcontroller animated: YES];
this might help you.
NextViewController *nextViewController=[[NextViewController alloc]initWithNibName:#"NextViewController" bundle:nil];
UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:nextViewController];
[self.navigationController presentModalViewController:navBar animated:YES];

How do I implement a UINavigationController in this case?

current version of my project :
I have 5 different UIViewControllers in my app. I've set my
FirstViewController to be the Initial View Controller using the
Attributes Inspector. I move back and forth from one ViewController to
another by using buttons to which I assign modal segues, from one
ViewController to another, using the StoryBoard
What I want to change:
I want to keep the navigation buttons obviously, delete the modal segues and use
a UINavigationController instead. If I understand the concept
correctly, when using a UINavigationController I need to go into each
UIButton-IBAction and at the very end of the method I have to push the next
ViewController I want to move to, onto my NavigationController (do I also
have to pop the current one first?). However, I can't figure out how
to implement all that correctly.
What I've done so far:
I removed all modal segues from the storyboard and kept the navigation buttons along with their corresponding IBActions
I unchecked the box in the Attributes Inspector that was making my FirstViewController the initial View Controller of my app
I went into my AppDelegate.m and tried to create the Navigation Controller there and make my FirstViewController be the RootViewController
MyAppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *myFirstViewController = [[FirstViewController alloc] init];
UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
[myNavigationController pushViewController:myFirstViewController animated:YES];
// Override point for customization after application launch.
return YES;
}
I then tried to test if the above was working by going into the IBAction of a
navigation button on my FirstViewController and implemented the
following in order to move to my SecondViewController when the
button is pressed :
FirstViewController.m
- (IBAction)goRightButton:(UIButton *)sender
{
// some code drawing the ButtonIsPressed UIImageView on the current View Controller
UIViewController *mySecondViewController = [[SecondViewController alloc] init];
[self.navigationController pushViewController:mySecondViewController animated:YES];
}
but nothing happens. What am I doing wrong ?
You are not linking your XIB file. Please add your navigation controller as
UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];
Use following code to move from one view to another
UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];
If you are using a storyboard, you should just drag in the navigation controller there and hook it up to your app delegates. As long as it is the main storyboard, and you have identified a view controller to load first, you do not need to load any views in your app delegate.
In order to push a view programmatically that's in a storyboard, you need to do something like the following:
//bundle can be nil if in main bundle, which is default
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyCustomViewController *customVC = (MyCustomViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"customVC"];
//standard way
[self.navigationController pushViewController:customVC animated:YES];
//custom animation
[UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
[self.navigationController pushViewController:customVC animated:NO];
} completion:nil];
You identify the view controller with the identifier you add in the storyboard editor. Below are some screenshots to help show what I mean.

How to setup view in UIViewController

I've created an almost-empty UIViewController class named MyViewController. In the viewDidLoad I'm setting the title and adding a close-button to the navigationItem.leftBarButtonItem.
I'm presenting my MyViewController like this:
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] myViewController];
nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:nc animated:YES];
When the viewController is presented, the background of it's view is just black. How can I setup it's view to fill-out the screen with an empty view -- just like when the UIViewController is setup in a Storyboard?
I've tried adding the following to the viewDidLoad, but the view is still black:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
By default a VC's view gets created in -loadView which you usually neither call, nor override. It gets called automatically the first time you request the the VC's view property.
The view's size is automatically set to the 'empty space', like everything except for the status bar without a NavigationController, minus the navbar when using one etc. You shouldn't worry about its size - usually it's just fine.
You can add your own views in -viewDidLoad and remove them again (for low-memory reasons) in -viewDidUnload.
You can add an UIImageView with custom image and then sent it to back
So in your viewcontrollers viewDidLoad
UIImageView *back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"MyBG"]];
[self.view addSubview:back];
[self.view sendSubViewToBack:back];
If you are using a XIB file you should init the view controller through the method initWithNibName: bundle:

Problems adding UIViewController to UINavigationController

My RootViewController is a UITableViewController. A UINavigationController is added programmatically:
_navigationController = [[[UINavigationController alloc] initWithRootViewController:_rootViewController] autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
Within the RootViewController.m the DetailViewController should be loaded when a row is selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Switch to detail");
CCouchDBDocument *selectedObject = [self.contentsList objectAtIndex:indexPath.row];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
[self.view addSubview:detailViewController.view];
[detailViewController setDetailItem: selectedObject];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
Without addSubView nothing happens on the screen. All the examples I've seen before only use pushViewController. And, loading the DetailView takes about 4 seconds. That's way too long (it's currently empty, just one label). When I try to set the navigationTitle (self.title = #"Hello";), the title remains the same from the RootViewController, so something must be wrong with the navigationController.
I tried to put everything in the AppDelegate and use a switchView method. The problem is the call for setDetailItem, which I can't call if I work with the switch method.
What would be the correct way to load the DetailView from the RootViewController into the navigation stack and possibly more from the DetailViewController later?
Update
I started from the beginning again with a Window-based application. Added a UITableViewController as "RootViewController" and initialised it with the UINavigationController in the AppDelegate (did absolutely nothing in the XIB). When I try to set self.navigationController.title = #"Test"; in ViewDidLoad, nothing happens.
What's wrong there?
You don't set the title of the DetailView when it's displayed using a UINavigationController by using self.title, you need to set the UINavigationItem title property in the DetailView initializer.
e.g. in the DetailView initializer :-
self.navigationItem.title = #"Hello";
You're right you shouldn't need to add the detailViewController view as a subview of the current view - you should just need the pushViewController call. I'm not sure why it's not appearing though.
Obvious questions are is everything connected OK in the nib, and what does the DetailView initializer do?