【发布时间】:2014-07-01 03:55:04
【问题描述】:
我正在学习 iOS 的 Objective-c,并且有一个关于创建我的第一个目标动作机制的问题。我已经让它工作了,但目前我只是将addTarget:action:changeForControlEvents: 方法的target: 部分设置为nil,这意味着它将在我的应用程序中搜索目标,而不是向下钻取ViewController.m,其中我要发送消息的方法位于。
如何告诉addTarget:action:changeForControlEvents: 方法首先搜索哪个类?
这是我当前代码的简单版本:
观点:
// View.m
#import View.h
@implementation
- (void)sendAction
{
UIControl *button = [[UIControl alloc] init];
[button addTarget:nil // How do I make this look for ViewController.m?
action:@selector(changeButtonColor)
changeforControlEvents:UIControlEventTouchUpInside];
}
@end
...和视图控制器:
// ViewController.m
#import ViewController.h
@implementation
- (void)target
{
NSLog(@"Action received!");
}
@end
感谢您的帮助!
【问题讨论】:
-
一种常见的方法是委托模式,它被广泛覆盖。有一篇很好的基本文章 here 或查看有关此主题的许多现存的 Stack Overflow 问题。
-
@rfarry 在这里你可以使用委托方法...
-
您可以通过提及目标作为对
ViewController类的引用来指向ViewController.m中的changeButtonColor。
标签: ios objective-c xcode uicontrol target-action