Programmatically creating UINavigationController i

2019-01-18 06:45发布

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.

7条回答
唯我独甜
2楼-- · 2019-01-18 07:29

For Swift 3.0, using filter:

let desiredController = self.navigationController!.viewControllers.filter { $0 is YourController }.first!
self.navigationController!.popToViewController(desiredController, animated: true)
查看更多
混吃等死
3楼-- · 2019-01-18 07:39

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;
}
查看更多
不美不萌又怎样
4楼-- · 2019-01-18 07:42
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

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-18 07:43

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

查看更多
贪生不怕死
6楼-- · 2019-01-18 07:44

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

查看更多
祖国的老花朵
7楼-- · 2019-01-18 07:46

You can creat UINavigationController in Appdelegate and set your first viewcontroller on it.

查看更多
登录 后发表回答