Iphone Uibutton事件 -(无效)我的按钮单击{ UGP=120; 对于(int i=0;i

Iphone Uibutton事件 -(无效)我的按钮单击{ UGP=120; 对于(int i=0;i,iphone,uibutton,Iphone,Uibutton,您没有在任何地方告诉按钮有关名称的信息。您可以使用[aa setTitle:name forState:uicontrol StateNormal];在标题按钮标题中设置它,然后使用[sender currentTitle]在全屏:中访问它 如果希望消息独立于标题,可以创建自己的UIButton子类来存储消息,或者为每个按钮设置不同的目标操作 此外,您构造名称的方式很脆弱。类似这样的方式可能更好: -(void)mybuttonclick{ UGFloat p=120; for(int

您没有在任何地方告诉按钮有关
名称的信息。您可以使用
[aa setTitle:name forState:uicontrol StateNormal];
在标题按钮标题中设置它,然后使用
[sender currentTitle]
全屏:
中访问它

如果希望消息独立于标题,可以创建自己的
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]);
}
NSArray* names = [NSArray arrayWithObjects:@"ZERO",
                                           @"ONE",
                                           @"Two",
                                           @"Three",
                                           nil];
for (NSString* name in names) {
    // Do stuff with name
}