的iOS如何使用pushviewcontroller与NavigationController(目标 - 三)

问题描述:

我想通过点击与NavigationController的iOS如何使用pushviewcontroller与NavigationController(目标 - 三)

所以按钮来更改视图我添加了一个按钮,Main.storyboard和写一些代码像

@property (weak, nonatomic) IBOutlet UIButton *button; 

在我ViewController.m

我加入方法

- (IBAction)buttonClicked:(id)sender { 

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
[self.navigationController pushViewController:secondViewController animated:YES]; 
(当我做我的项目自动创建)

}

(我做了SecondViewController.m,SecondViewController.h,SecondViewController.xib)

在此之后,我就开始应用,并点击该按钮后,屏幕并没有改变。

其实,当我加入日志像

NSLog(@"%@", self.navigationController); 

空印。

我想我需要将一些关于NavigationController的代码添加到AppDelegate.m中,但我不知道该怎么做。请帮帮我。

+0

您在故事板中是否嵌入了“导航控制器”?如果你没有故事板,你如何显示FirstViewController? – Satyam

试试这个,

在Appdelegate.m

`- (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]; 
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navigation; 
    [self.window makeKeyAndVisible]; 
    return YES; 
}` 

你需要通过代码或情节板中嵌入导航控制器。

首先在故事板中选择您的初始视图控制器并将其嵌入到NavigationController中。 enter image description here

然后给第二个视图控制器一个故事板标识符。

然后最后从storyboard实例化第二个ViewController并推送它。

- (IBAction)buttonClicked:(id)sender { 
SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; 
[self.navigationController pushViewController:entryController animated:YES]; 
}