我定义了点的函数
typedef void (^ButtonClick)(id sender);
我想在按钮单击时打电话给它(uibutton addtarget要调用 ^buttonClick函数)但是可以找到指针。
-(void)addRightButton:(UIImage*)btnImage click:(ButtonClick)click{
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[modalViewButton addTarget:self
action:@selector(click) // <==== could not find pointer.Pointer errors
forControlEvents:UIControlEventTouchUpInside];
// other code to add modelViewButton on View.....
}
-(void)test
{
[self addRightButton:[UIImage imageNamed:@"btn_shuaxin_1.png"] click:^(id sender) {
NSLog(@"it is test code");//<===never called
}];
}
如何使其成为SEL?
您无法获得这样的选择。(对于在运行时获得选择器,请使用sel_getUid()
函数)。另外,您会使选择器和块感到困惑。您想要的是可能的,但是使用其他方法:
- (void)addRightButton:(UIImage *)btnImage click:(ButtonClick)click{
UIButton *modalViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[modalViewButton addTarget:click
action:@selector(invoke)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)test
{
[self addRightButton:[UIImage imageNamed:@"btn_shuaxin_1.png"] click:^{
NSLog(@"it is test code");
}];
}