tags:

views:

232

answers:

5

I have created five buttons in a for loop dynamically. Now I want to associate an OnClick event with every button which do different actions. How I can do this and how can I detect which button is clicked?

for (NSUInteger i=0;i<5;i++)
{
    UIButton *myButton1 = [[UIButton buttonWithType:UIButtonTypeCustom] 
        initWithFrame:CGRectMake(5, 57,15, 15)];
    [myButton1 setTitle:@"Click Me!" forState:UIControlStateNormal];
    [myButton1 addTarget:self action:@selector(buttonClicked1:) 
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myButton1];
}
+1  A: 

You could asseble the selector name into a string and turn the string into a selector:

for (int i=0; i<5; i++)
{
    NSString *actionName = [NSString stringWithFormat:@"buttonClicked%i", i];
    SEL action = NSSelectorFromString(actionName);
    // …
}

But as the buttons will probably do something similar, it would be better if they all called the same method, where you would simply tell the buttons apart by the tag:

for (int i=0; i<5; i++)
{
    // …
    [button setTag:i];
    [button addTarget:self action:@selector(buttonClicked:)
        forControlEvents:UIControlEventTouchUpInside];
}

- (void) buttonClicked: (id) button
{
    const int tag = [button tag];
    switch (tag) { /* … */ }
}

By the way, why do so many people insist on writing NSInteger when you can simply type int? Is there a difference? No that I know of.

zoul
FWIW: "When building 32-bit applications, NSInteger is a 32-bit integer. A 64-bit application treats NSInteger as a 64-bit integer." http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/uid/20000018-DontLinkElementID_8
Sixten Otto
A-ha, there’s even a SO thread about them: http://stackoverflow.com/questions/13725.
zoul
A: 

Thanx a lot for you quick reply. I will check out it soon.

About you query of NSUInteger I also think so,but most of sample codes uses the same instead of int.

Thanx once again for your reply.

XcoderMi2
A: 

Hi Zoul,

The code is not working,because the tag of the button is set to the last incremented value of the variable.Is there any way to do this?

XcoderMi2
Please add your responses to someone's answer as a comment on that answer. SO is not a forum, it has a problem/answer structure.
JoostK
A: 

Why are you creating 5 buttons in a for loop? Is there any particular reason why you're not using Interface Builder?

Also, the code you posted will place each button at the same position. A button will be on top of the previous button, and so on.

Jasarien
A: 

Actually I have given a portion of the code and give the parameters manually.Sorry for that.

I will have to create the buttons dynamically based on some data comming from a .xml file.Thats why I can't use the interface builder I am giving the codes once again

for (NSUInteger i=0;i<76;i=i+15)

{ UIButton *myButton1 = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(i+5, 57,15, 15)]; [myButton1 setTitle:@"Click Me!" forState:UIControlStateNormal]; [myButton1 addTarget:self action:@selector(buttonClicked1:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:myButton1]; }

XcoderMi2