大佬教程收集整理的这篇文章主要介绍了在iOS上以编程方式向按钮添加操作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
[btn addTarget:self action:@SELEctor(indexAction) forControlEvents:UIControlEventTouchUpInside];
这很好用.但是,我现在想要将一个变量(int k)传递给indexAction方法.我无法让这个工作:
[btn addTarget:self action:@SELEctor([self indexAction:k]) forControlEvents:UIControlEventTouchUpInside];
我确信我做错了什么.这是上面代码的上下文:
-(void) iniTindexButtons { float buttonPadding = -8; float buttonWidth = 23; float buttonHeight = 80; CGRect frame = CGRectMake(0,0); for (int k=0;k<5;k++) { UIButton* btn = [[UIButton alloc] initWithFrame:frame];; btn.tag = k; btn.frame = CGRectMake(0,k*(buttonPadding+buttonHeight),buttonWidth,buttonHeight); UIImage *newImage = [UIImage imagenamed:@"index.png"]; [btn setBACkgroundImage:newImage forState:UIControlStateNormal]; [btn addTarget:self action:@SELEctor(self indexAction:k) forControlEvents:UIControlEventTouchUpInside]; } } -(void)indexAction:(int *)buttonTag { NSLog(@"buttonTag: %i",buttonTag); }
编辑:
我将代码更改为:
// ... [btn addTarget:self action:@SELEctor(indexAction:) forControlEvents:UIControlEventTouchUpInside]; //[self indexAction:k]; [indexView addSubview:btn]; [indexView sendSubviewToBACk:btn]; } } -(void)indexAction:(id)sender { NSInteger *tid = ((UIControl *) sender).tag; NSLog(@"buttonTag: %i",tid); }
但现在我收到警告消息“初始化从整数中生成指针,而不是针对行NSInteger * tid =((UIControl *)sender).tag;
编辑:
这是正确的代码行:
NSInteger tid = ((UIControl*)sender).tag;
以上是大佬教程为你收集整理的在iOS上以编程方式向按钮添加操作全部内容,希望文章能够帮你解决在iOS上以编程方式向按钮添加操作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。