2011-09-04 72 views
0

我想在另一个方法(而不是viewDidLoad)中设置我的页面标题。我的代码如下。无法设置页面标题的方法

基本上,'topicInfoLoader将从服务器检索数据并将其作为数组传回给应用程序。我使用NSLOG来验证主题的文本是否存在。但是,当我尝试在此方法中设置页面的标题时,它不起作用,即页面不显示标题。任何人都可以告诉我为什么会发生这种情况,我该如何解决这个问题?

- (void) getData 
{ 
    //Get info regarding the topic 
    NSString *topicInfoResourcePath = [NSString stringWithFormat:@"/topics/%@",self.topic._id]; 

    Loader *topicInfoLoader = [[Loader alloc]initWithResourcePath:topicInfoResourcePath]; 

    [topicInfoLoader setCompletionHandler:^(NSArray *anArray){ 
     self.topicInfo = [anArray lastObject]; 

     //NSLog shows that topic title value exists 
     NSLog(@"topic title :%@",self.topicInfo.text); 

     //Set the page title 
     self.title = self.topicInfo.text; 
}]; 

回答

0

我通常设置在我ViewController的初始化标题和正常工作。

此外,您在不属于自己的实例方法内调用self.title。我想你从self打电话的地方其实是topicInfoLoader,而不是你的ViewController。或者你的ViewController是topicInfoLoader

+0

实例方法'getData'属于'self'。我正在调用另一个类的实例'topicInfo'的完成块。在返回块中,我可以执行各种操作,但似乎视图控制器无法识别返回块中其标题的设置。任何想法为什么? – Zhen

+0

'topicInfo'是你的ViewController的一个属性吗?你能从该完成块内的ViewController中访问任何东西吗?你为什么不在完成块之外做'self.title'? – rjgonzo

+0

因为我需要的信息只能在完成块中获得。由于它是异步的,所以我不知道什么时候可以得到它,因此将self.title放在外面可能无法正常工作。 – Zhen