2

ボタンを押すとストーリーボードのビューが変わるように機能するこのコードがあります。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view1"];
[self presentViewController:viewController animated:YES completion:nil];

このコードは機能しますが、もう一度使用すると、次を使用して元のビューに戻ります。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"view2"];
[self presentViewController:viewController animated:YES completion:nil];

動作せず、次のエラーが表示されます。

Warning: Attempt to present <UIViewController: 0x863ad30> on <UITabBarController: 0x86309b0> whose view is not in the window hierarchy!

識別が設定されており、私は私の機能ではviewdidloadないので、これを修正する方法がわかりません。

誰でも助けることができますか?

4

3 に答える 3

19

これを試して、

最初の方法

-(IBAction)signin:(id)sender
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewController"];
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];

}

2 番目の方法 (前のビューに戻る)

- (IBAction)signout:(id)sender
{

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"parentViewController"];
    [[[[UIApplication sharedApplication] delegate] window] setRootViewController:vc];

}
于 2013-11-10T09:02:45.460 に答える
5

これは、 を呼び出す前にメソッドを呼び出すと発生する可能性があるmakeKeyAndVisibleため、呼び出しをその後に移動します。

そうでない場合は、以下のハックを試してください。

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:viewController animated:YES completion:nil];
于 2013-07-06T14:30:13.617 に答える
1

エラー メッセージはすべてを示しています。View Controller を表示する前に、View Controller のビューをウィンドウに追加する必要があります。

UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: viewController.view];
[self presentViewController:viewController animated:YES completion:nil];
于 2013-07-06T14:24:42.547 に答える