Ios 加载视图后设置UIViewController视图属性

Ios 加载视图后设置UIViewController视图属性,ios,objective-c,Ios,Objective C,我正在构建一个需要重新加载视图以显示不同类型问题的应用程序 我有QuestionView,所以每次用户重新加载时,我都会创建一个新的QuestionView,并将self.view更改为新视图 如果我在主队列上执行所有操作,那么它就会工作 执行self.view=newView后,视图将更改: //This will work. The UI will be change to the new view [ProgressHUD show:@"Loading question"]; Questi

我正在构建一个需要重新加载视图以显示不同类型问题的应用程序

我有QuestionView,所以每次用户重新加载时,我都会创建一个新的QuestionView,并将self.view更改为新视图

如果我在主队列上执行所有操作,那么它就会工作

执行self.view=newView后,视图将更改:

//This will work. The UI will be change to the new view
[ProgressHUD show:@"Loading question"];
QuestionView *newQuestionView = [self getQuestionView]; //a time wasting task
self.view = newQuestionView;
[ProgresHUD dismiss];
但是,这不会改变用户界面:

//not sure why these code won't update the UI
[ProgressHUD show:@"Loading question"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     QuestionView *newQuestionView = [self getQuestionView]; //a time wasting task
     dispatch_async(dispatch_get_main_queue(), ^{
         [ProgressHUD dismiss];
         self.view = newQuestionView;
     });
});

我当然更喜欢第二种方法。有没有明确要求控制器重新绘制视图的想法?

问题的核心是,分配给self.view并不会将视图添加到窗口中,一般来说,这是一个坏主意

不要尝试替换viewcontroller的视图,而是将视图放在self.view中

[questionView removeFromSuoerview];

// setup new question view here

questionView.frame = self.view.bounds;
[self.view addSubview:questionView];

或者,您可以使用
transitionFromView:toView:duration:options:completion:
通过一些漂亮的过渡动画从旧问题过渡到新问题。

但是您知道为什么第一个代码有效而第二个代码无效吗?它将在LoadView或viewDidLoad中有效,因为它们尚未添加到windows视图中层次结构,因此“右”视图被添加到层次结构中。之后再执行此操作将不起作用,因为指定给self.view本身不会将视图添加到窗口的视图层次结构中。