0

ViewController という名前のビュー コントローラーがあります。

  • ViewController は、ボタンを持つ UIView を表示します。これにより、2 番目のビュー コントローラーである SecondViewController に進むことができます。
  • SecondViewController にもボタンがあり、3 番目のビュー コントローラーに進むことができます。

ただし、ThirdViewController の表示に問題があります。SecondViewController でボタンをタップすると、エラーがスローされます。

警告: ビューがウィンドウ階層にない ... で ... を表示しようとしています!

この問題に対処する他のサイトや投稿をたくさん見てきましたが、有効な解決策が見つからないようです。かなりの数のソリューションを実装しましたが、どれも機能していないようです。

これが私のコードです:

- (void)secondaryView:(UIView *)secondaryView
{
    UIView *theView = secondaryView;

    UIViewController *viewController = [[UIViewController alloc] init];
    viewController.view.frame = [UIScreen mainScreen].bounds;

    [viewController.view addSubview:theView];
    viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:viewController animated:YES completion:nil];
}

secondaryViewアプリケーションの他の場所で構築している UIView です。それをviewControllerに追加してから、viewController.

UIViewControllers を動的に作成し、それぞれに UIView を追加し、それらをウィンドウ階層に追加する方法はありますか?

4

1 に答える 1

1

ViewControllers でナビゲーションを使用できる場合は、以下のコードを試すことができます

firstViewController を呼び出す場所で、

   -(void)callFirst
    {
    FirstViewController *first = [FirstViewController alloc] init];
    UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:first];
    navigationController.modalPresentaionStyle = UIModalPresenationFormSheet;
    [self presentModalViewController:navigationController animated:YES];
}

また、FirstViewController ボタン アクションでは、次のように記述できます。

-(void)callSec
{
SecondViewController *sec = [SecondViewController alloc] init];
[self.NavigationController pushViewController:sec animated:YES];
}

そして SecondViewController でも同じことができます

  -(void)callThird
    {
        ThirdViewController *third = [ThirdViewController alloc] init];
        [self.NavigationController pushViewController:third animated:YES];

    }

これを試してください...そして、これらのViewControllersでは、次のような要件を満たすために必要なコーディングを行うことができます

-(void)viewDidLoad
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(20,20,400,400)];
newView.backGroundColor = [UIColor redColor];
[self.view addSubView:newView];
}
于 2013-05-15T07:10:21.903 に答える