gpt4 book ai didi

iphone - 如何从子 UIViewController iPhone 应用程序重命名父 UIViewController 标题?

转载 作者:行者123 更新时间:2023-11-29 11:08:50 26 4
gpt4 key购买 nike

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

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

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

从这个子 UIViewController,我必须将父 viewController 的标题名称从“All Messages”更新为“New Messages(1)”,后退按钮名称也应从“All Messages”更改为“New Messages(1)” )”

我已按照以下代码更新屏幕标题。此代码位于我名为 Conversation screen 的 subview Controller 中。

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

谁能帮帮我哪里做错了?这种做法是否正确?请帮我做这件事。提前致谢。

最佳答案

尝试使用NSNotificationCenter

AllMessagesViewController中声明一个方法:

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

然后为通知事件注册 View Controller

[[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;
}

关于iphone - 如何从子 UIViewController iPhone 应用程序重命名父 UIViewController 标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12494567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com