self.title返回(null)

时间:2009-10-27 15:38:40

标签: iphone objective-c uinavigationcontroller

使用导航控制器,我正在推动一个新视图并动态设置它的标题:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int countryIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *selectedCountry = [[countrysData objectAtIndex: countryIndex] objectForKey: @"title"];
scheduleState *scheduleStateViewController = [[scheduleState alloc] initWithStyle:UITableViewStyleGrouped];

[self.navigationController pushViewController:scheduleStateViewController animated:YES];    
scheduleStateViewController.title = selectedCountry;    

[scheduleStateViewController release];

CountryData是一个从xml feed创建的可变数组。

现在当我转到下一个视图时,它显示正确,但是当我尝试在新的viewController中执行NSLog时,它会记录为(null):

NSLog(@"The title is: %@", self.navigationItem.title);

2009-10-27 11:30:20.416 myApp [50289:20b]标题是:(null)

我需要使用标题作为获取Web服务查询的参数...

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果您尝试在控制器的-viewDidLoad或类似内容中记录标题,则会失败,因为您在推送视图控制器后设置了标题。在推动控制器之前设置控制器的标题,它应该更好。

唯一的另一个原因可能是nil是selectedCountry变量是否为零。直接记录以确保。