开发者

How to create button in a loop and assign each button a specific tag

i want to create a cluster of b开发者_如何转开发uttons.for this i am using a loop like this.

for (int i =0; i< [plistArray count];i++) {

    editButton = [[UIButton alloc]initWithFrame:CGRectMake(width-10, -3.6, 39, 35)];
    [editButton setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
}

but when i clicked on any random button assigned function is not called.It called when i clicked first button(in a sequence 1,2,3).


Here's one:

    #define ButtonHeight 40

    #define OffsetBetweenButtons 20

    #define ButtonHeightPlusOffsetBetweenButtons (ButtonHeight+OffsetBetweenButtons)

//create 6 buttons from 0,1,2,3,4,5 so totally 6 buttons
for(int buttonIndex=0;buttonIndex<=5;buttonIndex++){

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self 
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"Button" forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal];

    //Set offset in Y axis
    button.frame = CGRectMake(20.0, ButtonHeightPlusOffsetBetweenButtons * buttonIndex , 280.0, 40.0);

    //Set Tag for future identification
    [button setTag:buttonIndex];

    [YourView addSubview:button];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜