开发者

IBAction used with Dynamically added UIbuttons

Good Evening all!

I have some UIButtons added dynamically into my view and of course I have an IBAction which handles button events.

The problem is: How can I detect which button is pressed if the only thing I know is the (id)sender and the array of buttons?

Buttons are never the same, every button has a different behavior. When I want to use static buttons and connect them through the IB I use something like this :

-(IBAction)doSomething:(id)sender
{
    if(sender == button1)
        dosomething;
    if(sender == button2)
        dosomething else;
    if(sender == button3)
        dosomething3;
}

In my case this does not work because there is no button1, button2, button3 but a MutableArray of buttons which have the same name as they were allocated with it. Button!

I tried using the way above but with no success and i tried also getting the tag of a button but I have nothing to compare it to!

I would really appreciate your help.

sincerely

L_Sonic PS Dynamicaly means that i am creating the buttons in random time during run time like this

-(void)configActionSheetView
{
    buttonView = [[UIView alloc]initWithFrame:CGRectMake(0.0,460, 60, 480)];
    [buttonView setBackgroundColor:[UIColor blackColor]];
    [buttonView setAlpha:0.6];
    for (int i = 0 ;i<[buffButtons count];i++)
    {
        UIButton *customButton = [buffButtons objectAtIndex:i];
        customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //UILabel   *customLabel = [[UILabel alloc]init];
        //[customButton setTag:(i)+11];
        [customButto开发者_StackOverflow社区n addTarget:self action:@selector(activateBuffEffect:) forControlEvents:UIControlEventTouchUpInside];
        [customButton setAlpha:1.0];
        customButton.frame = CGRectMake(8.0, 5+(50*i), 44.0, 44.0);
        [customButton setTitle:nil forState:UIControlStateNormal];
        buttonView.frame = CGRectMake(0, 460, 60, 50+(44*(i+1)));
        [buttonView addSubview:customButton];
    }
}

this is inside a functions and gets called during run time. the buffButtons is a mutableArray with buttons that gets populated during runtime. i need a solution like this i cannot get a different eventhandling method for everybutton.


When you was "added dynamically" I assume you mean that they are created from some piece of code. Since all buttons to different things and you know what a certain button should do, why don't you add different actions to different buttons?

UIButton *myCreatedButton = [[UIButton alloc] init];
[myCreatedButton addTarget:self 
                    action:@selector(doSomething:) 
          forControlEvents:UIControlEventTouchUpInside];

UIButton *myOtherCreatedButton = [[UIButton alloc] init];
[myOtherCreatedButton addTarget:self 
                         action:@selector(doSomethingElse:) 
               forControlEvents:UIControlEventTouchUpInside];

In the above code the target (set to self) is the class where the method you want to run is found, the action is the method that you want to run and the controlEvent is what should cause the method to run.

If you did it like this you would split the code in different methods like these (you do not need to specify them in the header):

-(void)doSomething:(id)sender {
    // do somthing here ...
}

-(void)doSomethingElse:(id)sender {
    // do somthing else here ...
}

This way you don't need to know what button was pressed since the correct code would get called anyway. Besides it makes it cleaner if you need to change the code for some of the buttons.


Found it!

-(IBAction)buttonTapped:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSLog(@"tapped: %@", btn.titleLabel.text);
    [self anotherIBAction:sender];
}

now i can get the tag from the btn :D thnk you!


Why not add a tag the button and then get the tag number from (id)sender in the selector function?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜