2017-06-17 4 views
3
私はMain.storyboardにボタンを追加NavigationControllerので

のiOS(客観 - C)

でボタンをクリックすることで表示を変更し、

@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を作った)、私は、アプリケーションを起動して、ボタンをクリックしたが、画面は変化しませんでした。実際

、私はnullが印刷された

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

のようなログを追加しました。

NavigationControllerについてAppDelegate.mにいくつかのコードを追加する必要があると思いますが、何をすべきか分かりません。私を助けてください。

+0

あなたのストーリーボードに「ナビゲーションコントローラ」を埋め込んでいませんか?ストーリーボードがない場合、FirstViewControllerをどうやって見せていますか? – Satyam

答えて

3

は、あなたがコードやストーリーボードによるナビゲーションコントローラを埋め込む必要がある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; 
}` 

、これを試してみてください。

1

最初にストーリーボードで最初のviewcontrollerを選択し、NavigationControllerに埋め込みます。 enter image description here

次に、第2のビューコントローラにストーリーボード識別子を与えます。

最後に、ストーリーボードからSecond ViewControllerをインスタンス化して押します。

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