问题
I've just updated my Xcode from 4.2 to 4.3.3 and i keep coming across a problem is it possible to add a navigation controller in a single view application because when i try to embed one into the controller nothing happens. I would like to have two view controllers connected by a button to the second controller and a navigation bar back to the first view controller.
I can't think of any other way to connect the view controllers please help me any-ideas.
回答1:
If you don't want to add a navigation controller, you could transition between your existing view controllers using
presentViewController
to go from the first one to the second one, anddismissViewControllerAnimated
to return.Assuming you're using a NIB (otherwise you'd just use the embed command for the storyboard), if you want to add a navigation controller staying with your NIB, you can alter your app delegate accordingly.
So, you probably have an app delegate that says something like:
// AppDelegate.h
#import <UIKit/UIKit.h>
@class YourViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) YourViewController *viewController;
@end
Change this to add a navigation controller (you can get rid of the previous reference to your main view controller here):
// AppDelegate.h
#import <UIKit/UIKit.h>
//@class YourViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
//@property (strong, nonatomic) YourViewController *viewController;
@property (strong, nonatomic) UINavigationController *navigationController;
@end
And then, in your app delegate's implementation file, you have a didFinishLaunchingWithOptions
that probably says something like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You can change that to say:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
YourViewController *viewController = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Having done that, you can now navigate from one NIBs view controller to another's using pushViewController
and return with popViewControllerAnimated
. In your viewDidLoad
you can also use the self.title = @"My Title";
command to control what appears in the view's navigation bar. You may also want to change the "Top Bar" property in your NIBs to include the navigation bar simulated metric, so that you can layout your screen and have a good sense of what it's going to look like:
Clearly, if you've got a non-ARC project, those lines with the alloc/init of the view controllers should also have an autorelease
, too (which will be obvious when you look at your app delegate).
来源:https://stackoverflow.com/questions/11341337/embed-navigation-controller