简体   繁体   English

iOS:从viewDidAppear更改视图

[英]iOS: change view from viewDidAppear

I'm playing around with view life cycles & am having trouble changing a view from the load of a different view. 我正在围绕视图生命周期进行操作,并且无法从其他视图的负载中更改视图。

In my ViewController.hi have: 在我的ViewController.hi中有:

-(void)viewDidAppear:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController2 *viewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
    [self presentViewController:viewController animated:YES completion:nil];
}

However this only causes the view to be between ViewController, and ViewController2 appearing with animation (in a loop). 但是,这只会导致视图介于ViewController和ViewController2之间,并带有动画(循环显示)。

I used the code in viewDidLoad however neither of the view's loaded (from reading you cannot change view until the viewWillAppear) 我在viewDidLoad使用了代码,但是视图均未加载(从读取开始,直到viewWillAppear都无法更改视图)

Update: When using the code in viewWillAppear , whose view is not in the window hierarchy error is thrown. 更新:在viewWillAppear使用代码时, whose view is not in the window hierarchy将引发错误。

How does one change the view from view setup stage? 如何从视图设置阶段更改视图?

Update Using the above code, inside & out of GCD, in viewDidLoad , viewWillAppear & viewDidAppear either results in an infinite loop of animated showing of the ViewController2, or crash on 2nd attempt of segue (result from the loop). 更新使用上述代码,在GCD内部和外部,在viewDidLoadviewWillAppearviewDidAppear要么导致ViewController2的动画显示无限循环,要么导致第二次segue尝试崩溃(从循环中得到)。

EDITED: 编辑:

I'm not sure exactly what you're trying to do, but assuming you are wanting the first viewcontroller to appear and then the second viewcontroller to immediately animate on top of the first one, you should be able to accomplish using several options: 我不确定您要做什么,但是假设您希望第一个ViewController出现,然后让第二个ViewController立即在第一个ViewController上进行动画处理,则应该可以使用以下几个选项来完成:

First you could just wrap your calls in a dispatch_async call: 首先,您可以将调用包装在dispatch_async调用中:

dispatch_async(dispatch_get_main_queue(), ^{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController2 *viewController = (ViewController2 *)[storyboard 
    instantiateViewControllerWithIdentifier:@"ViewController2"];

    [self presentViewController:viewController animated:YES completion:nil];
});

Or you could use a show modally segue: 或者,您可以使用节目以模态方式进行搜索:

- (void)viewDidLoad {
  [super viewDidLoad];

  dispatch_async(dispatch_get_main_queue(), ^{

        [self performSegueWithIdentifier:@"myModalSegue" sender:self];
  });
}

Or you could use a navigation controller and use a standard show segue (formally push). 或者,您可以使用导航控制器并使用标准的show segue(正式推送)。 This one doesn't require the dispatch_async: 此代码不需要dispatch_async:

- (void)viewDidLoad {
  [super viewDidLoad];

  [self performSegueWithIdentifier:@"myshowsegue" sender:self];
}

I've posted working examples of all three on: github 我已为所有三个工作例子上: github上

It is better to exchange views in loadView method. 最好在loadView方法中交换视图。

- (void)loadView {
    CGRect rect = [[UIScreen mainScreen] applicationFrame];
    MyView *view = [[MyView alloc] initWithFrame:rect];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    self.view = view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM