I am new to iOS. And I want to use navigation controller in my application but I have no any idea how to do it. So can any one guide me step by step for creating navigation in my application.
问题:
回答1:
In appDelegate.h
@property (strong, nonatomic) UINavigationController *navController;
and set the delegate UINavigationControllerDelegate
and synthesise object in appDelegate.m
now,
appDelegate.m
you can set navigation controller in didFinishLaunchingWithOptions
method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
frstVwCntlr = [[firstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.frstVwCntlr];
self.window.rootViewController = self.navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In the above code , your firstViewController is set to UINavigationController
and UINavigationController
added to UIWindow
like
self.window.rootViewController = self.navController
Hope this may help you
回答2:
If you want to create everything programmatically , you have to do it in AppDelegate.
But if you don't want to do it programmatically, then just select the ViewController in Storyboard then select menu options :
Editor->Embed In -> Navigation Controller
回答3:
You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.
回答4:
So for creating a UINavigationController programatically without using storyboards, go to your app delegate and do the following. Create two properties, window and viewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor clearColor];
self.viewController = [[YourFirstViewController alloc] initWithNibName:@"YourFirstViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
回答5:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];
where ImageViewController2 is a class name
回答6:
Here is the code that you should write in app delegate.
UIViewController *vc=[[UIViewController alloc]initWithNibName:@"vc1" bundle:nil];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
view.backgroundColor=[UIColor redColor];
[vc setView:view];
self.navme=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = self.navme;
回答7:
For Swift 3.0, using filter:
let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)