是否有相当于“焦点监听器”的东西?在 Objective-C 或 iPhone SDK 中? (来自爪哇)

发布于 2024-08-27 01:40:27 字数 294 浏览 4 评论 0原文

我是一名学生程序员,我在空闲时间学习了 Objective-C,因为我的大学不教授它。到目前为止我们只使用过 Java 和基本的 C。我正在为 iPod 编写一个程序,想知道是否有任何类型的方法可以像 Java 中的 Focus Listener 那样调用类中的方法?我有一个视图,当视图放置在顶部并再次可见时,我想调用刷新方法(以更新从另一个视图新输入的按钮标题)。这是否太简单了,还是有更系统的方法来做到这一点?我尝试从其他视图类调用该方法,但它似乎不起作用(表示另一个类未定义或可能不接受方法调用并在执行时崩溃)。

任何见解将不胜感激。感谢您抽出时间。

I am a student programmer who has taken up Objective-C on my free time as my college doesn't teach it. We have only used Java and basic C so far. I am in the middle of making a program for the iPod and was wondering if there was any type of way to call a method in a class similar to the way a Focus Listener does in Java? I have a view that I would like to call a refresh method (to update the newly inputted titles of buttons from another view) when the view is put at the top and visible again. Is this too easy or is there a more methodical way of doing that? I have tried to just call the method from the other view class but it does not seem to work (says the other class is either undefined or may not accept the method call and crashes on execution).

Any insight would be appreciated. Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2024-09-03 01:40:28

我不知道我是否很好地理解了这个问题,但是当视图可见时,会在 UIViewController 中调用“viewDidAppear:”方法。

否则,NSNotification 相当于 Objective-C 中的监听器模式。你可以像这样添加一个观察者:

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

被观察者像这样处理他的消息:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"aNotification" object:nil]];

I don't know if I have understood the issue very well but when a view is being visible the "viewDidAppear:" method is call in the UIViewController.

Otherwise the equivalent of the Listener pattern in Objective-C is the NSNotification. You can add an Observer like that:

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

And the Observed handled his message like that:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"aNotification" object:nil]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文