如何从Child UIViewController iPhone应用程序重命名Parent UIViewControll



我在基于UITabBarController的iPhone应用程序中工作。我的应用程序有5次点击。在我的第4次点击中,UIViewController有一个子UIViewController。我的第一个RootViewController屏幕标题是"所有消息"。我的第二个UIViewController名称是"Conversations"。

屏幕流程为点击4->所有消息->对话

所有消息都包含UITableView,因此如果用户单击表视图中的任何消息,我们将把用户带到对话屏幕(UIViewController)。此屏幕显示特定用户的消息。

从这个子UIViewController,我必须将Parent viewController的标题名称从"All Messages"更新为"New Messages(1)",并且后退按钮名称应从"All Messages(所有消息)"更改为"New Message(1))">

我已经按照下面的代码更新了屏幕标题。这段代码在我的"孩子"视图控制器"对话"屏幕中。

AllMessagesViewController *mesViewController = [[AllMessagesViewController alloc] init];
mesViewController.navigationItem.title = @"New Messages(1)";

有人能帮帮我哪里做错了吗?这种方法是正确的吗?请帮我做这个。提前谢谢。

尝试使用NSNotificationCenter

AllMessagesViewController中声明一个方法:

- (void)changeTitle:(NSString *)newTitle;

然后为通知事件注册视图控制器

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(changeTitle:) 
name:@"ChangeTitleEvent" object:nil];

然后,当您想更改ConversationViewController的标题时,发送一个通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTitleEvent"
object:@"New Message (1)"];

此方法将调用AllMessagesViewController中的changeTitle:方法,因此您只需使用参数newTitle:更改标题

- (void)changeTitle:(NSString *)newTitle
{
self.title = newTitle;
}

这样做:

NSArray *arrControllers = [self.navigationController viewControllers];
if([[arrControllers objectAtIndex:[arrControllers count]-2] isKindOfClass:[AllMessagesViewController class]])
{
AllMessagesViewController *objAllMessagesViewController = (AllMessagesViewController *)[arrControllers objectAtIndex:[arrControllers count]-2];
objAllMessagesViewController.title = @"New Messages(1)";
}

最新更新