将多个 argu fun 分配给动态按钮时应用程序崩溃

发布于 2024-10-03 13:40:47 字数 724 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再浓的妆也掩不了殇 2024-10-10 13:40:48

试试这个:

for (int i=0;i<[tableDataSource count];i++)
{
    UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [btn setTitle:rowText forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(myActionbtnText:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = i;
    btn.frame = CGRectMake(60,  40+2*(40*i), 200, 40);
    btn.alpha = 0.81;

    [self.view addSubview:btn];
}

-(void) myAction:(UIButton*)sender {
    NSDictionnary *dict = [tableDataSource objectAtIndex:sender.tag];
    NSString* rowText = (NSString*)[dict objectForKey:@"title"];
    NSLog(@"%@ Button Clicked",rowText);
}

Try this :

for (int i=0;i<[tableDataSource count];i++)
{
    UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [btn setTitle:rowText forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(myActionbtnText:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = i;
    btn.frame = CGRectMake(60,  40+2*(40*i), 200, 40);
    btn.alpha = 0.81;

    [self.view addSubview:btn];
}

-(void) myAction:(UIButton*)sender {
    NSDictionnary *dict = [tableDataSource objectAtIndex:sender.tag];
    NSString* rowText = (NSString*)[dict objectForKey:@"title"];
    NSLog(@"%@ Button Clicked",rowText);
}
北凤男飞 2024-10-10 13:40:48

您的解决方案有几个问题。

  1. UIButton 不响应 addTarget:action:withObject:forControlEvents:,而是响应 addTarget:action:forControlEvents。这就是您的应用程序崩溃的原因。
  2. 选择器应该是@selector(myAction:)。不应包含该参数。
  3. 我非常确定它无论如何都不会起作用,因为按下按钮时 rowText 将超出范围并且不再可用。这就是为什么你应该使用 @MathieuF 解决方案(只需确保正确指定选择器)。

There are several things wrong with your solution.

  1. UIButton does not respond to addTarget:action:withObject:forControlEvents: but rather addTarget:action:forControlEvents. This is why your application is crashing.
  2. The selector should be @selector(myAction:). The parameter should not be included.
  3. I'm pretty sure that it wouldn't work anyway since by the time the button is pressed 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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文