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

时间:2014-04-04 17:32:30

标签: 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"];
QuestionView *newQuestionView = [self getQuestionView]; //a time wasting task
self.view = newQuestionView;
[ProgresHUD dismiss];

但是,这不会改变UI:

//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;
     });
});

我肯定更喜欢第二种方法。任何想法明确要求控制器重绘视图?

1 个答案:

答案 0 :(得分:0)

问题的核心在于,分配给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:从旧问题过渡到新问题,并使用一些不错的过渡动画。