I am working with push notifications. i am trying to create and push a DetailView in the navigationController when action button in notification is clicked. but navigationController is nil. how can i put that DetailView in the navigationController? I want to push RootViewController in the navigationController and then the DetailView. how can i do that?
in AppDelegate:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
RootViewController *controller = [[RootViewController alloc] init];
//getting warnings here.(Unused variable navigationController)
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[controller doStuff];
[controller release];
}
in RootViewController:
-(void)doStuff{
[[self stories] removeAllObjects];
[self startParsing];
[self.tableView reloadData];
DetailViewController *detail = [[DetailViewController alloc] init];
//custom code
[self.navigationController pushViewController:detail animated:YES];
[detail release];
this is the code i m using right now. and plz notice that i have
[self.tableView reloadData];
Okay, now I am pretty sure I understand the issue. The problem is, you never manually set the navigationController property on a UIViewController. The navigationController property is nil if the view controller is not under a navigation controller and if it is, then the property points to it.
What you need to do, is when you display your root view controller, instead of directly displaying its view, add it to a navigation controller, then display the navigation controller's view. Like so:
RootViewController *controller = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
//Here you would display navigationController.view somehow
So, after you have your root view controller in a navigation controller, in a method in root view controller you can do this:
DetailViewController *detail = [[DetailViewController alloc] init];
//Do whatever you need to do to set values on the detail view controller
[self.navigationController pushViewController:detail animated:YES];
The key thing is this: you need to put the root view controller into a navigation controller before you can access a navigation controller from within root view controller.
If your nav controller is nil, it needs creating and then you place things into it.
Try this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:detailViewController animated:NO]; // or YES
If you start out with no UINavigationController and want to display it with more than one view controller in its stack, after initing the navigation controller you can set the viewControllers property to an array with the various view controllers you want.
edit: some example code:
UINavigationController *navController = [[UINavigationController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[navController setViewControllers:viewControllers animated:NO];
After doing that, your navigation controller will have viewController2 on top and viewController1 behind it.
Alternately, you could do it this way:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navController pushViewController:viewController2 animated:NO];
Related
I have an alternative flow in my app. This flow starts in my firstViewController, then in this view a call my secondViewController like this:
- (IBAction)PressButton:(id)sender {
SecondViewController *second = [[SecondViewController alloc] init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *nav = self.navigationController;
[nav presentViewController:second animated:YES completion:nil];
}
In my secondViewController I want to push my thirdViewController. But it is not working I tried this ways:
- (IBAction)pressButton:(id)sender {
ThirdViewController *tvc = [[ThirdViewController alloc] init];
UINavigationController *nav = self.navigationController;
[nav pushViewController:tvc animated:YES];
}
When I press the button of secondViewController nothing happens.
What I'm doing wrong ?
I'm using:
OSX 10.8.2
Xcode 4.6
iOS 6.1
You must present the navigation controller modally, and have the second view as the root of that navigation controller. As well as calling presentViewController from the owning view not its parent navigation controller.
- (IBAction)PressButton:(id)sender {
SecondViewController *second = [[SecondViewController alloc] init];
second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:second];
[self presentViewController:navigationController animated:YES completion:nil];
}
Instead of presenting just the second view controller, make sure to present an additional navigation controller.
SecondViewController *secondViewController = [[SecondViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[[self navigationController] presentViewController:navigationController animated:YES completion:nil];
If you are using storyboard just click on the source view xib, Ctrl+drag to destination (Create Segue), select modal from popup menu.Click on the newly created connection. Add a name for it then in source view controller [self performSegueWithIdentifier:#"Segue Name" sender:self];
If you are trying to get a button to direct you to a different page modally, you can go into the storyboard or xib file. Control click from that button to the view controller you want to go to. and then the popup menu will give you the options of what type of outlet you want to use. Hope this helps
I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.
I have a problem building applicatin with tabBarController.
There is no problem doing tabBarController with navigationController if I build it from AppDelegate.
But now I have experienced problem when I want to create new view with tabBarController (3 tabs and each has navigation controllers) after a push from previous navigation controller.
It simply doesnt work.
Here is the code:
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainView_iPhone" bundle:nil];
mainViewController.tabBarItem.title = #"First";
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
DictionariesViewController *dictionariesViewController = [[DictionariesViewController alloc] initWithNibName:#"DictionariesView_iPhone" bundle:nil];
dictionariesViewController.tabBarItem.title = #"Second";
UINavigationController *dictionariesNavigationController = [[UINavigationController alloc] initWithRootViewController:dictionariesViewController];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:mainNavigationController, dictionariesNavigationController, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
There is a problem after view is pushe to "First" controller. Application crashes...
Please for help.
Regards
Borut
What are you trying to do with the following code?
[self.navigationController pushViewController:tabBarController animated:YES];
You said that your app has 3 tabs and each of those tabs have a navigation controller. Therefore, what you should do is to add the navigation controllers to tabBarController.viewControllers (which you did), but then you need to set the tabBarController as the root view controller.
I have done it this way and it works:
registerViewController = [[RegisterViewController alloc] initWithNibName:#"RegisterView_iPhone" bundle:nil];
AppDelegate_Phone *delegatePhone = [[UIApplication sharedApplication] delegate];
[delegatePhone.firstViewController.navigationController pushViewController:registerViewController animated:YES];
Thanks for your help guys.
Hi I have a modalViewController that I am popping up using
[self presentModalViewController:myController animated:YES];
I have an event occurring within myController which I would like to result in another controller being pushed onto the navigation stack ON TOP OF myController (which again has been presented modally). How can I do this?
I have tried the following from within myController:
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self];
NewController* n = [[NewController alloc] init];
[navController pushViewController:n animated:YES];
[n release];
This does not work however....
First create your second modalViewController
NewController* new = [[NewController alloc] init];
then create navigaitonController like this
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController: new];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
then present your navigationController as modalview controller
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
Here you go. Hope it helps.
If i understand right you want to display new navigation stack on top of modal view.
If it is right I think it won't be possible. Modal view is a top one. Even if you'll push new ViewController to the "parent" navigation stack - it won't be available until you'll quite from your modal view.
I have am setting up my application like so (in applicationDidFinishLaunching):
mytable = [[[MyTableController alloc] initWithStyle:UITableViewStylePlain] retain];
UINavigationController *mynav = [[[UINavigationController alloc]initWithRootViewController:mytable] autorelease];
[mynav.view setFrame:CGRectMake(0,0,320,460)];
UIViewController *tab1 = [[tabBarController viewControllers] objectAtIndex:0];
[mytable setTitle:#"Chronological"];
mytable.navigationController = mynav;
[tab1.view addSubview:mynav.view];
[window addSubview:tab1.view];
where MyTableController extends UITableController and has a navigation controller property. tabBarController is an outlet via the main nib file. There are no other nib files.
I am now unable to add any buttons to the navigation controller. Everything I do is ignored. What am I doing wrong here?
Can you include the code where you set up the UITabBarController tabBarController? I'm guessing that you are not properly setting the viewControllers property. Use UITabBarController -setViewControllers:animated: with an array of view controllers to initialize the tab bar controller.
Try something like this:
mytable = [[MyTableController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *mynav = [[UINavigationController alloc] initWithRootViewController:mytable];
[tabBarController setViewControllers:[NSArray arrayWithObject:mynav] animated:NO];
[mynav release];
[mytable release];
[tabBarController viewWillAppear:NO];
[window addSubview:[tabBarController view]];
[tabBarController viewDidAppear:NO];