将多个 argu fun 分配给动态按钮时应用程序崩溃
for (int i=0;i<[tableDataSource count];i++)
{
NSDictionary *dict = [tableDataSource objectAtIndex:i];
rowText = [dict objectForKey:@"title"];
UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[btn setTitle:rowText forState:UIControlStateNormal];
[btn addTarget:self action:@selector(myActionbtnText:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(60, 40+2*(40*i), 200, 40);
btn.alpha = 0.81;
[self.view addSubview:btn];
}
我在将操作分配给动态按钮时遇到错误,下面给出了我的操作,如何在按钮事件发生时传递对象/参数。
-(void) myAction:(NSString *)btnText;
{
NSLog(@"%@ Button Clicked",btnText);
}
for (int i=0;i<[tableDataSource count];i++)
{
NSDictionary *dict = [tableDataSource objectAtIndex:i];
rowText = [dict objectForKey:@"title"];
UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[btn setTitle:rowText forState:UIControlStateNormal];
[btn addTarget:self action:@selector(myActionbtnText:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(60, 40+2*(40*i), 200, 40);
btn.alpha = 0.81;
[self.view addSubview:btn];
}
I got error at assign Action to dynamic button my action is given Below, how to pass object/parameter when button event occurs.
-(void) myAction:(NSString *)btnText;
{
NSLog(@"%@ Button Clicked",btnText);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this :
您的解决方案有几个问题。
addTarget:action:withObject:forControlEvents:
,而是响应addTarget:action:forControlEvents
。这就是您的应用程序崩溃的原因。@selector(myAction:)
。不应包含该参数。rowText
将超出范围并且不再可用。这就是为什么你应该使用 @MathieuF 解决方案(只需确保正确指定选择器)。There are several things wrong with your solution.
addTarget:action:withObject:forControlEvents:
but ratheraddTarget:action:forControlEvents
. This is why your application is crashing.@selector(myAction:)
. The parameter should not be included.rowText
will be out of scope and no longer available. Which is why you should go with @MathieuF solution (Just make sure to specify the selector correctly).