First off I'm new at programming. I'm creating an app with one navigation bar controller. The app is pretty basic except for a quiz section that is comprised of 12 xibs. As the users takes the quiz a new xib is pushed onto the stack. I want to create a button that takes the user back to the home screen if they do not want to complete the quiz. The back button takes them to the previous xib which could be 11 deep. Is it possible to dismiss the modal views and reload the rootView controller?
Here's the code from my delegate
#synthesize window;
#synthesize navigationController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
-(void)dealloc {
[window release];
[navigationController release];
[super dealloc];
}
This is how I'm pushing new xibs onto the stack
-(IBAction) showTesting: (id)sender {
Testing *varTesting = [[[Testing alloc] initWithNibName:#"Testing" bundle:nil] autorelease];
[[self navigationController] pushViewController:varTesting animated: YES];
}
Any help would be greatly appreciated. Thanks
You can just call popToRootViewControllerAnimated: to go back to the first view controller.
Related
Before:
My App is based on indepent view controllers. I can switch from one to another by replacing the root view controller on the application delegate:
ade.window.rootViewController = newController;
... and all worked right, till now.
Tomorrow:
we have to add a NavigationController-based part of our App, which will help the users navigate through our:
Brands => Model Names => Colors
So, the user will choose a color, then click a button: now I will switch to another UIViewController (call it "pippo"), which actually resides outside that navigation hierarchy (I can't push it in the nav-controller for several methods, I'm forced doing so!).
What I want is to get back to my "Color" screen, from "pippo". So, I'm looking for a way to programmatically "navigate" the navigation controller I restore, I mean:
I restore my navigation controller
now I'm on Brands, but I don't want my users to be here, I want to show them the last color they was on (I saved it in the preferences)
how can I simulate the selection of a known brand and model?
Thanks a lot.
In applicationDidFinishLoading in App delegate:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[window makeKeyAndVisible];
[window addSubview:navController.view];
That will instantiate the navigation controller and add it to the window as a view.
Now, in your rootViewController class (lets say its called FirstViewController) you can do this:
- (void)clickedAButton:(id)selector {
SecondViewController *nextViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
// and push it onto the 'navigation stack'
[self.navigationController pushNavigationController:nextViewController animated:YES];
// and release
[nextViewController release];
}
And in your SecondViewController you can navigate back through the stack using:
- (void)clickedAnotherButton:(id)selector {
// goes back to the last view controller in the stack
[self.navigationController popViewControllerAnimated:YES];
}
So for you it would go:
Set up navigation controller in the app delegate with Brand as the root view controller
User chooses their brand and you pushViewController:animated: the Model view controller. Then the user chooses their model and you pushViewController:animated: the Color view controller. Similarly the user chooses a color and you push the Pippo view controller. Now, if the user presses back (or you call popViewControllerAnimated:) it will go back to the Color view controller in the same state as when the user left it to go to the Pippo controller.
Write the following code in AppDelegate.m Class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.nav = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.nav.navigationBarHidden = YES;
[mainViewController release];
[_window addSubview:nav.view];
[_window makeKeyAndVisible];
}
Folks,
New to iPhone development so I greatly appreciate your help. I have been looking at documentation and trying many things to get my navigation controller functionality working but can't.
Here's the situation. I have an app delegate which sets its root controller to be a navigation controller:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.mainViewController.navController;
[self.window makeKeyAndVisible];
return YES;
}
The navigation controller is a property of the mainviewcontroller because I intend to take the user to a map view (associated with a mapviewcontroller) only when they select an item in a tableview owned by mainviewcontroller. I can't yet think of a graceful way to notify the appdelegate about a table row selection, hence I am trying to handle this in the mainviewcontroller.
In the mainviewcontroller init function:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom initialization
navController = [[UINavigationController alloc] initWithRootViewController:self];
mapController = [[MapViewController alloc] init];
}
return self;
}
In mainview controller, when the user selects an item from the tableview, I call:
[self.navController pushViewController:mapController animated:YES];
The view associated with my mapviewcontroller never appears. Any ideas what's going on?
Thanks
Add this
[self.window addSubview:self.mainViewController.navController.view];
Before
[self.window makeKeyAndVisible];
I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(#"Button pressed") is called, but the view won't load. Here is my code:
- (IBAction)pushAbout {
NSLog(#"Button pressed");
AboutView * aboutView = [[AboutView alloc] initWithNibName:#"AboutView" bundle:nil];
[self.navigationController pushViewController:aboutView animated:YES];
[aboutView release];
}
I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.
The class files "AboutView" were created as UIViewController subclass. They were created and left untouched
A guess: Your view is not in a UINavigationController, hence
self.navigationController
is actually nil, and nothing happens.
The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.
A simple way of doing this:
In your app delegate, change
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController* navController = [[UINavigationController alloc]
initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)
Yes you don't have a UINavigationController set.
Add
NSLog(#"nav? %#", self.navigationController);
to your action and you'll see that it dumps (null).
However, the AboutView.xib works fine if you enter this code:
[self presentModalViewController:aboutView animated:YES];
instead of
[self.navigationController pushViewController:aboutView animated:YES];
The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.
You can dismiss the presented modal view by using
[self dismissModalViewControllerAnimated:YES];
in your AboutViewController.
To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.
I have created a new application using the View Based Application Template in xCode.
My first view, which displays on loading the app, is a button based menu. One of the buttons (Make) should load a new navigation stack using a NavigationViewController. There are other buttons that when implemented will do other things outside of NavigationViewController's scope.
I would like to be able to click Make and open and display a new navigation controller.
From ViewController.m:
-(IBAction)makeStory:(id)sender{
NSLog(#"makeStory:");
navController = [[UINavigationController alloc] init];
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:#"MakeStoryTableViewController" bundle:nil];
[navController pushViewController:makeStoryTableViewController animated:YES];
}
I have created a NavigationViewController in the opening ViewController.h file:
#import <UIKit/UIKit.h>
#import "MakeStoryTableViewController.h"
#interface StoryBotViewController : UIViewController {
UINavigationController *navController;
MakeStoryTableViewController *makeStoryTableViewController;
}
- (IBAction)makeStory:(id)sender;
#end
I know I'm missing something because when I call pushViewController nothing happens - I think that somehow I have to attach the NavigationViewController to the ViewController that makeStory: is in.
For reference, my app delegate header declares the view controller in the #implementation as follows:
UIWindow *window;
StoryBotViewController *viewController;
with the appropriate #synthesize in the .m of the app delegate
#synthesize window;
#synthesize viewController;
How do I push a NavigationStack on from my opening view controller?
Please forgive me if the question is a little vague, I'm happy to provide more information if you need it. It's my first time questioning on stackoverflow and I'm obviously a bit of a newbie with the iPhone SDK.
you had it almost. But you forgot to add the NavigationController to your view.
[self.view addSubview:navController.view];
Add this in your IBAction.
EDIT:
but much likely this is not what you want, because you can't navigate back to your very first viewController. If you want to navigate back to the first viewController set up your project like in the navigation-based template. You can to this programmatically to:
Move all UINavigationController stuff from the header file of the viewcontroller to the app delegate header. And change your app delegate implementation to something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// Add the view controller's view to the window and display.
// [window addSubview:viewController.view];
[window addSubview:navController.view];
[window makeKeyAndVisible];
return YES;
}
make sure to release navController in dealloc
then replace your IBAction with something like this:
- (IBAction)makeStory:(id)sender {
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:#"MakeStoryTableViewController" bundle:nil];
[self.navigationController pushViewController:makeStoryTableViewController animated:YES];
}
if you don't like to display the navigationbar in your first viewcontroller hide it, but make sure to unhide it if you push other items on the stack. self.navigationController.navigationBar.hidden = ...
I am very new to Obj-C and learning iphone development.
My question is how to add subview from app delegate.
Lets say I added subview called "MainView" from "applicationDidFinishLaunching" method.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MainViewController *aViewController = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
self.mainViewController = aViewController;
[aViewController release];
[window addSubview:mainViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
"MainView.xib" file has a button to show its child view. When the button is clicked, it calls "showChildView" method.
- (IBAction)showChildView:(id)sender {
if (self.childViewController == nil) {
ChildViewController *childController = [[ChildViewController alloc] initWithNibName:#"ChildView" bundle:nil];
self.childViewController = childController;
[childController release];
}
[self.view insertSubview:childViewController.view atIndex:0];
}
From this code, when app launches, it shows "MainView" with a button. But when I clicked the button, the button is still visible as well as the content from the "ChildView.xib" file too.
How can I hide the "MainView" when I pressed the button and show only the contents of the "ChildView"?
Thanks for your help in advance.
well, you have to remove the original view first, before inserting the new subview, do it this way
- (IBAction)showChildView:(id)sender {
if (self.childViewController == nil) {
ChildViewController *childController = [[ChildViewController alloc] initWithNibName:#"ChildView" bundle:nil];
self.childViewController = childController;
[childController release];
}
[self.mainViewControlle.view removeFromSuperView];
[self.view insertSubview:childViewController.view atIndex:0];
}
Hope this helps.
You might want to check out the Utility App sample -- it demonstrates switching between two views with animation and adding/removing views from parent views.
you might want to create a navigation controller in the main view and than push the childviewcontroller onto it when invoking showChildView. You'll get the back navigation button for free that way