Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从UIButton子类@implementation调用方法_Iphone_Ios4_Call - Fatal编程技术网

Iphone 从UIButton子类@implementation调用方法

Iphone 从UIButton子类@implementation调用方法,iphone,ios4,call,Iphone,Ios4,Call,我在一个使用Xcode 4的iPhoneiOS4项目中工作 我对UIButton进行了子类化,这样它就可以拦截单点和双点点击 这是UIButton子类@implementation的最后一部分,这是两个实例方法,其中点击被“记录” 在nib中创建了一个按钮实例,一切正常:它截取单点击和双点击并输出NSLog 现在的问题是:我的ViewController中有两个方法(resetAllFields和populateAllFields),我需要单点击执行resetAllFields和双点击执行pop

我在一个使用Xcode 4的iPhoneiOS4项目中工作

我对UIButton进行了子类化,这样它就可以拦截单点和双点点击

这是UIButton子类@implementation的最后一部分,这是两个实例方法,其中点击被“记录”

在nib中创建了一个按钮实例,一切正常:它截取单点击和双点击并输出NSLog

现在的问题是:我的ViewController中有两个方法(resetAllFields和populateAllFields),我需要单点击执行resetAllFields和双点击执行populateAllFields

我该怎么办?我把电话放在哪里


谢谢。

如果要处理ViewController中的行为,典型的解决方案是在自定义按钮类中添加一个
@protocol
,该类定义了处理单点击和双点击的方法

i、 e.在自定义按钮中

@protocol CustomButtonDelegate <NSObject>
    - (void)button:(CustomButton *)button tappedWithCount:(int)count;
@end
id <CustomButtonDelegate> _delegate;
视图控制器实现协议方法,并将自身设置为自定义按钮的委托

即,在ViewController实现中

- (void)button:(CustomButton *)button tappedWithCount:(int)count {
     if (count == 1) {
         [self resetAllFields];
     } else if (count == 2) {
         [self populateAllFields];
     }
}

由于您使用的是Interface Builder来设置自定义按钮,因此您可以在其中或ViewDiLoad中将视图控制器指定为代理。

谢谢。我是个新手,所以我需要更多关于代表的解释。我怎样才能创建它们呢?哎呀,现在我看到了你的完整答案。谢谢。如何在viewDidLoad中声明代理?我尝试了[CustomButton setDelegate:self];但是get warning CustomButton可能不会响应SetDelegates该按钮在interface builder中设置正确吗?如果是这样,那么您可以通过插座获取。也就是说,self.customButton.delegate=self。此外,请确保为委托设置了@属性。
- (void) handleTap: (UITapGestureRecognizer *) sender {
   NSLog(@"single tap");
   [self.delegate button:self tappedWithCount:1];
}

- (void) handleDoubleTap :(UITapGestureRecognizer *) sender {
   NSLog(@"double tap");
   [self.delegate button:self tappedWithCount:2];
}
- (void)button:(CustomButton *)button tappedWithCount:(int)count {
     if (count == 1) {
         [self resetAllFields];
     } else if (count == 2) {
         [self populateAllFields];
     }
}