Uibutton事件 [英] Uibutton events

查看:121
本文介绍了Uibutton事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(void)mybuttonclick{
UGFloat p=120;
    for(int i=0;i<3;i++){   
        UIButton *aa = [UIButton buttonWithType:UIButtonTypeCustom];  
        if(i==0){        
            NSString *name=[[NSString alloc]initWithString@"ZERO"];
        }else if(i==1){
            NSString *name=[[NSString alloc]initWithString@"ONE"];
        }else if(i==2){
            NSString *name=[[NSString alloc]initWithString@"Two"];
        }else{
            NSString *name=[[NSString alloc]initWithString@"Three"];
        }
    [aa setFrame:CGRectMake(0.0f, 0.0f, 500.0f, 40.0f)];
    [aa setCenter:CGPointMake(100.0f,p)];
    [aa setBackgroundColor:[UIColor blueColor]];
    [aa addTarget:self action:@selector(fullscreen:)   forControlEvents:UIControlEventTouchUpInside];   
    [self.window addSubview:aa];
    p=p+50; 
    }
}

//----------------fullscreen method--------------------
-(void) fullscreen:(id)sender{

    NSLog(@"button pressed on %@",[sender Stringvalue]);
}

这里我动态创建了3个UIButtons。当我按下第一个按钮时,我想显示 ZERO 。当按下第二个时,打印 ONE ...然后两个,然后。我怎样才能做到这一点?我知道可以使用选择键工作完成,但我不知道如何。

Here I created 3 UIButtons dynamically. When I press the first button I want to display ZERO. When pressing the second one, print ONE... then Two, then Three. How can I do this? I know it can be done using selector key work, but I don't know how.

推荐答案

你不是在说任何地方关于名称的按钮。您可以使用 [aa setTitle:name forState:UIControlStateNormal]; 在标题按钮标题中设置它,然后在全屏中访问它:使用 [sender currentTitle]

You aren't telling the button about name anywhere. You could set it in the title buttons title with [aa setTitle:name forState:UIControlStateNormal]; then access it in fullscreen: with [sender currentTitle].

如果您希望邮件独立于标题,您可以制作你自己的 UIButton 的子类来存储消息,或者为每个按钮设置不同的目标动作。

If you want the message to be independent of the title you could either make your own subclass of UIButton to store the message or have a different target action for each button.

此外,你构建名字的方式很脆弱。这样的事情可能会更好:

Also, the way you are constructing the names is fragile. Something like this might be better:

NSArray* names = [NSArray arrayWithObjects:@"ZERO",
                                           @"ONE",
                                           @"Two",
                                           @"Three",
                                           nil];
for (NSString* name in names) {
    // Do stuff with name
}

这篇关于Uibutton事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆