I'm newbie. There is a button in ViewController.m . When I press the button it has to navigate to SecondViewController but SecondViewController doesn't appear.
And at the SecondViewController on the navigation bar, there will be "Back" button to go back to ViewController. Can you tell me what I'm missing?
ViewController.m:
-(IBAction)buttonPressed:(id)sender
{
EnglishViewController *v = [[[EnglishViewController alloc]
initWithNibName:#"EnglishViewController"
bundle:nil]
autorelease];
[self.navigationController pushViewController:v animated:TRUE];
}
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
The solution (depends on #George's answer):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *navCon =[[[UINavigationController alloc]
initWithRootViewController:self.viewController]
autorelease];
self.window.rootViewController =navCon;
[self.window makeKeyAndVisible];
return YES;
}
When you create your ViewController in AppDelegate.m you must surrond it with navigationController to make it work
Something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[navigation release];
[self.window makeKeyAndVisible];
return YES;
}
The first few things to check: is your -buttonPressed method actually getting called? Is are you getting and passing a non-nil value to -pushViewController:animated:? Is self.navigationController actually non-nil when you make that call?
Your code looks fine as written.
Related
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I do not know what is wrong with this method.I just created a new project and run it.It showing Applications are expected to have a root view controller at the end of application launch
If you have a MainWindow.xib.
remove below line. do not remove MainWindow.xib
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
OR
do not remove above line. remove MainWindow.xib and Project Summary Main Interface set null.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
In my project the navigation bar is being coming only in rootview(homeview) for the first only,i want to enable navigation bar in all views?Here my code?What change should i do for that
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Please help me to code?
See this link
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
navigationController =[[UINavigationController alloc] init];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[navigationController pushViewController:viewController2 animated:NO];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
And when you navigate to the another view from viewController2 do pushViewController not others like presentView
[self.navigationController pushViewController:anotherViewController animated:YES];
Put this code in a delegate class.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Set the navigation controller as the window's root view controller and display.
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
If you want to enable navigation in all the views then you will have to declare the navigation in AppDelegate.m wherein it will be viewable in all the views. I dont have my mac rite now but its the best advice I can provide for now :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.window.rootViewController =navigationController;
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Change the uiviewcontroller to SecondViewController
try this way may be helped you
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// [NSThread sleepForTimeInterval:0.1]; // simulate waiting for server response
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
// Initialise the navigation controller with the first view controller as its root view controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
// This is where we hide the navigation bar! :)
[navigationController setNavigationBarHidden:NO];
// Navigation controller has copy of view controller, so release our copy
//[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[self.viewController release];
// Add the navigation controller as a subview of our window
[_window addSubview:[navigationController view]];
[_window makeKeyAndVisible];
return YES;
}
I got this code on didFinishLaunching but I don't know how to customize it to open up menuViewController from when I open the app.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// At the end of applicationDidFinishLaunching, right before
// the return YES
[[GCTurnBasedMatchHelper sharedInstance] authenticateLocalUser];
return YES;
}
hope someone can help me out
Change the following line
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
to
self.viewController = [[[menuViewController alloc] initWithNibName:#"menuViewController" bundle:nil] autorelease];
The line you need to change is
self.window.rootViewController = self.viewController;
To
MenuViewController *menuViewController = [[[MenuViewController alloc] init] autorelease];
self.window.rootViewController = menuViewController;
This is assuming you have a custom view controller called MenuViewController
Create the object of menuViewController assign this controller to rootViewController.
self.viewController = [[[MenuViewController alloc] initWithNibName:#"menuViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
I choose the project template as "Single View" .I am trying to push a detail view(UIViewController) on the button click.Button action is firing properly and executing the code [self navigatingController] push......My code is As:
MapDetailViewController *mapDetailViewController = [[MapDetailViewController alloc] init];
[self.navigationController pushViewController:mapDetailViewController animated:YES];
NSLog(#"self.navigation is as %#",self.navigationController);
self.navigationController is null.So i tried to alloc init a local variable of type Navigation Controller and also the variable was able to be non-null(some hex value), still i was unable to push the mapDetailView.
what i am thinking is that i have choosen the wrong template(view based).Should i choose the Navigation-based(Master-detail).Well the below auto generated code is of application delegate:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
Should some changes be made here for navigation controller?
Any Suggestion?
Your problem is that you don't even have a navigation controller to do the pushing. Change your code to this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
How to set class(first class) for UINavigationController from code ?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
LoginViewController* loginViewctrl = [[LoginViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewctrl];
UIWindow* window addSubview:[navigationController view]];
[loginViewctrl release];
[window makeKeyAndVisible];
}