如何在根视图控制器上设置标签的文本
问题描述:
从堆栈中的视图调用下面的代码之前或之后我想在程序将弹出的根视图上设置标签的文本。如何在根视图控制器上设置标签的文本
[self.navigationController popViewControllerAnimated:YES];
我能做到这一点的其他方式(如下)推视图控制器到堆栈的时候,但我不知道如何做到这一点从堆栈弹出
- (PushedViewController *) pushedViewController {
NSLog(@"Initialise view");
if (pushedViewController == nil) {
pushedViewController = [[PushedViewController alloc] initWithNibName:@"PushedView" bundle:nil];
}
return pushedViewController;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[TableView deselectRowAtIndexPath:indexPath animated:YES];
Table* tableRow =[fetchedResultsController objectAtIndexPath:indexPath];
[self.navigationController pushViewController:self.pushedViewController animated:YES];
self.pushedViewController.code.text = tablerow.code;
}
时如果有人能告诉我如何在堆栈中访问和设置视图的变量而不创建视图的新实例,那将是值得赞赏的。
答
你真正想要的是做一个委托式的回调。所以,你的孩子控制器将有
@protocol PushedViewControllerDelegate
@required
- (void)controller:(PushedViewController *)controller didUpdateSome:(id)data;
@end
和财产
@property (nonatomic, assign) id<PushedViewControllerDelegate> delegate
现在,您可以将根控制器分配到子控制器的delegate
,它可以通知回要求的任何变更。
'[[[navigationController rootViewController] textLabel] setText:@“Foo”];' – 2012-08-12 08:17:55
@ H2CO3当我这样做,我得到错误“没有已知的选择器rootViewController的类方法”也将允许我设置文本一个文本框? – msec 2012-08-12 08:28:49
对不起,我的意思是说方法名称为topViewController。 – 2012-08-12 08:36:01