gpt4 book ai didi

ios - 弹出 View 时传递参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:00 27 4
gpt4 key购买 nike

我正在构建一个 ios 应用程序,其中有两个 View AB 之间的导航。

导航模式是:

ViewController A >>> PushViewController >>> ViewController B

ViewController A <<< PopViewController <<< ViewController B

我希望当 B 返回到 A 时,A 相应地更新一些 UI 元素。例如,A View Controller 显示一些带有文本的标签,在 B 用户修改文本,当 View 弹出时,我希望 A 到更新并反射(reflect)更改。

问题是:A 如何知道它何时从 B 弹出? A 如何获取 B 传递的数据以便更新内容?解决此类问题的良好做法是什么?

谢谢

最佳答案

您可以使用 NSNotificationCenter 轻松完成此操作:

第一个 View Controller :

// Assuming your label is set up in IB, otherwise initialize in viewDidLoad
@property (nonatomic, strong) IBOutlet UILabel *label;

- (void)viewDidLoad
{
[super viewDidLoad];

// Add an observer so we can receive notifications from our other view controller
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateLabel:) name:@"UpdateLabel" object:nil];
}

- (void)updateLabel:(NSNotification*)notification
{
// Update the UILabel's text to that of the notification object posted from the other view controller
self.label.text = notification.object;
}

- (void)dealloc
{
// Clean up; make sure to add this
[[NSNotificationCenter defaultCenter]removeObserver:self];
}

第二 View Controller :

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];

NSString *updateLabelString = @"Your Text Here";
// Posting the notification back to our sending view controller with the updateLabelString being the posted object
[[NSNotificationCenter defaultCenter]postNotificationName:@"UpdateLabel" object:updateLabelString;
}

关于ios - 弹出 View 时传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337521/

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