i have created 4 dynamic buttons but how to write method on each of them
for (i = 1; i <= [a1 count]-1; i++)
{
NSString *urlE=[a1 objectAtIndex:1];
NSLog(#"url is %#",urlE);
#pragma mark buttons
CGRect frame = CGRectMake(curXLoc, 10, 60, 30);
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setImage:[UIImage imageNamed:#"tab2.png"] forState:UIControlStateNormal];
[button setTitle:(NSString *)#"new button" forState:(UIControlState)UIControlStateNormal];
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
curXLoc += (kScrollObjWidth1);
[self.view addSubview:button];
}
-(void)buttonEvent:(id)sender {
NSLog(#"new button clicked!!!");
if (sender == ??) how to tell button 1 ,2,3,4
{
}
}
You should give a .tag to each button on creation
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = i; // <-- here
...
With this, you could identify the button by .tag.
-(void)buttonEvent:(UIButton*)sender {
NSLog(#"new button clicked!!!");
if (sender.tag == 2) {
NSLog(#"2nd button clicked.");
...
You can specify a separate selector for each button using NSSelectorFromString to dynamically generate selector names.
For example
NSString *selectorName = [NSString stringWithFormat:#"button%dEvent:", i];
[button addTarget:self action:NSSelectorFromString(selectorName) forControlEvents:UIControlEventTouchUpInside];
-(void)button1Event:(UIButton*)sender {}
-(void)button2Event:(UIButton*)sender {}
-(void)button3Event:(UIButton*)sender {}
-(void)button4Event:(UIButton*)sender {}
Related
I have dynamically created some list of RadioButtons with some values. pro grammatically I changed the button state and changed the image for selected and unselected. but the problem is i can select the all radioButtons at same time. actually I need to select one at a time.
when I clicked the next RadioButton, previously selected button state should be changed to non-selected.
Here is my code, I tried with changing image, but ...some problem with my code.
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender ;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
where can I change the image of previously selected button.
thanks in advance
Deselect all the buttons when you select one. If you have buttons in your scroll view you can user this code:
//Your Method
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender;
[self deselectAll];
[self radiobuttonAction:RadioButton1];
}
- (void) deselectAll : (UIScrollView *) scrollView{
NSArray *viewArray = [scrollView subviews];
for (UIView *v in viewArray){
if([v isKindOfClass:[UIButton class]]){
[((UIButton *)v) setSelected:NO];
}
}
}
EDIT: But if you want to give it real radio button effect (in which one is always selected, and only one is selected) it will be more easy to you. Use following code:
//A globle refButton
UIButton *refButton = nil;
//Set image for both state:
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
//make any of above default select: may be the last one and pass that to `refButton`
refButton = RadioButton;
//Your Method
-(void)RadioButtonTapped:(id)sender
{
[refButton setSelected:NO];
refButton = (UIButton*)sender;
[refButton setSelected:YES];
}
Tag the button at the time of creation.
When button tapped, get the buttons through tag. Deselect all buttons. Update current button which you have received as a n argument.
assuming you want to add 5 radio buttons.
- (void)viewDidLoad
{
for (int i = 0; i < 5; i++) {
UIButton* RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0, i * 40)];
// [RadioButton setTag:i * 10];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
}
//do not forget to declared this in your header file
-(void)RadioButtonTapped:(UIButton*)button;
{
for (UIButton *btn in self.scrollView.subviews) {
[btn setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
}
[button setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
the code above updates the UI, sets the image of all buttons to "uncheck" then changes the image of the last pressed button to "check".
create buttons like this,
for(int i=0;i<4;i++)
{
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
RadioButton.tag = i*100;
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
add action like this,
-(void)radiobuttonAction:(UIButton *)Button
{
UIButton *Button = (UIButton*)sender;
for(UIButton * btn in self.scrollview.subViews)
{
if(btn.tag == Button.tag)
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
else
{
//do selected or de-selected code for other buttons
}
}
I am Creating UIButtons programmatically and Then Store these UIButtons in NSMutableArray. All these UIButtons have Some Text as Titles. I have No Outlet for These UIButtons. Now I want to Assign the Title of UIButton to UIlabel when I touch or Click some UIButton. But the problem for me is this: how can I perform the function on these UIButtons. Because I created UIButtons programmatically and also these UIButtons have No Outlet. Here is my code to create Buttons:
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<30; i++) {
if (btnn>8) {
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(200.0 , spacey, 30.0, 30.0);
int idx;
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle:titre1 forState:UIControlStateNormal];
spacey=spacey+30;
spacex = 80;
btnn = 0;
}
else {
btnn++ ;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(spacex, spacey, 30.0, 30.0);
int idx;
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle:titre1 forState:UIControlStateNormal];
spacex = spacex + 30;
[saveBtn addObject:btn];
[self.view addSubview:btn];
}
}
Please any one can guide me how can perfom action for these NSMutableArray UIButtons.
You can add actions to an UIButton programmatically using:
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonClicked:(UIButton *)btn
{
[btn setTitle:#"UnAutreTitre" forState:UIControlStateNormal];
}
Hope this helps.
Simply set tag and add target to the button. Later, you can get which button was tapped.
Create the action for the UIButton action, where you will be sending the (id)sender to the actionButtonFunction.
Assign sender to UIButton and then take the button title in NSString.
-(UIAction)buttonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.text;
// further code goes here
}
I have a:
#property(nonatomic, retain) NSArray * buttonsArray;
...
...
#synthesize buttonsArray;
when the view loads I initialize it as:
buttonsArray = [[NSArray alloc] initWithObjects:
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
[UIButton buttonWithType:UIButtonTypeRoundedRect],
nil];
// this code places the buttons from buttons array on top of the images in my view. I placed those images in an array called imagesArrayV;
int counter = 0;
counter=0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
[button setTag:counter]; // *********
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
CGRect tempRect = tempImage.frame;
button.frame = tempRect;
[self.ViewMainV addSubview:button];
counter++;
}
the purpose of this is to save time creating all the buttons in xcode and creating the connections.
I posted a picture so that you can get an idea...
Anyways the method that get's executed when clicking a button is:
-(void) test: (id) sender{
UIButton*btn = (UIButton*)(sender);
int tagnumber = [btn tag];
NSLog(#"%i",tagnumber);
}
why is it that when I press on the buttons the tag is equal to 0 when I set it to something else ( look for : // ********* ) when creating the button. Moreover when I run this method:
-(void) someOtherMethod{
int counter = 0;
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
button.alpha = 0;
button.titleLabel.text = #"I change the title";
counter++;
}
}
the buttons that I previously added do not change at all. also the alpha does not change. I don't know what button's I am changing when I run the last method.
Just below the line where you set the tag, you have the line button = [UIButton buttonWithType:UIButtonTypeRoundedRect];. This overwrites the button obviously. The action is then added to the newly created button, the buttons in the array are left unaltered.
Personally I would rewrite the code as follows:
for (int counter = 0; counter < numberOfButtons; ++counter) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTag:counter];
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
CGRect tempRect = tempImage.frame;
button.frame = tempRect;
[self.ViewMainV addSubview:button];
[buttonsArray addObject:button];
}
This also avoids populating the array hardcoded, for more flexible, less error-prone code.
try using this code instead of the third section above:
for(int i=0;i<[buttonsArray count];i++){
UIButton *button=[buttonsArray objectAtIndex:i];
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Hello" forState:UIControlStateNormal];
UIImageView *tempImage = [imagesArrayV objectAtIndex:counter];
button.frame = tempImage.frame;
button.tag =i;
[self.ViewMainV addSubview:button];
}
one of the issues, is that you were setting the tag on button, then replacing the button instance on the next line.
This looks suspicious, and you do it twice:
for (UIButton *button in buttonsArray) {
button = [buttonsArray objectAtIndex:counter];
You should not modify the loop enumeration variable button inside the loop. You simply use button as it is.
IOW, you either do:
for (counter = 0; counter < buttonsArray.count; counter++)
{
UIButton *button = [buttonsArray objectAtIndex: counter];
button.alpha = 0;
// etc...
or you simply get rid of counter and do:
for (UIButton * button in buttonsArray)
{
button.alpha = 0;
// etc...
I want to create uibuttons dynamically. so i for loop to create button with tag and added to view.
all buttons performs the same action with respect to its tag value... but i want to change the propery of the button. Therefore i need to get the uibutton using the tag value...
my code...
UIButton *button2;
//view did load
int width = 30;
for(int i = 1; i <= 5; i++)
{
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[UIImage imageNamed:#"star1.png"] forState:UIControlStateNormal];
button2.tag = i;
button2.backgroundColor = [UIColor clearColor];
button2.frame = CGRectMake(width, 78, 15, 15);
[self.view addSubview:button2];
width = width +30;
}
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
}
Use viewWithTag function of UIView to access your UIButton using the tag value.
See in Documentation viewWithTag
Use it as below.
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:1];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:3];
UIButton* myButton = (UIButton*)[mySuperView viewWithTag:6];
.........
-(void)ratingAction:(id*)sender
{
// here using the tag value i want to get the button and change the background image….
// for example i want to change the background for tag values 1,3,6,7 ,8…
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *temp=(UIButton*)sender;
if ([temp tag]==1 || [temp tag]==3 || [temp tag]==6 || [temp tag]==7 )
{
[temp setBackgroundColor:[UIColor redColor]];
}
}
}
Check this code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int y = 100;
for (int i=0; i<=5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80, y, 160, 40);
[button setTitle:[NSString stringWithFormat:#"Button-%i", i] forState:UIControlStateNormal];
// [button set
[self.view addSubview:button];
y=y+60;
}
}
-(void)aMethod:(id)sender{
UIButton *button = sender;
int y = 100;
int i = 5;
for (int x=0; x<=i; x++) {
NSString *frameString = [NSString stringWithFormat:#"{{80, %i}, {160, 40}}", y];
if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) {
NSLog(#"button %i clicked..", x);
}
y= y+60;
}
}
I have created buttons dynamically and now I want to set the background for a particular index of the buttons.
Here is my sample snippet:
In the interface file:
UIButton *answerBtn;
#property (nonatomic, retain) UIButton *answerBtn;
for(int i = 0; i < [myArray count]; i++) {
answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[answerBtn setFrame:CGRectMake(30, x, 260, 40)];
answerBtn.tag = i;
[answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
[self.view addSubview: answerBtn];
}
In my case, I want to set the button background in different methods.
-(void) custom Method
{
if(indexValue == correctIndex) // Values are 2
{
// so I want to set the background image for the second button
[answerBtn setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png"] forState:UIControlStateNormal];
}
}
But it doesn't set the corresponding index, so how can I do that?
Try this
for(int i = 0 ; i < [myArray count] ; i++ )
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:i];
[btn setFrame:CGRectMake(30, x, 260, 40)];
[btn addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (IBAction) btnClicked::(id)sender
{
UIButton *btnTapped=(UIButton *)sender;
for(UIView *btn in self.view.subviews)
{
if([btn isKindOfClass:[UIButton class]])
{
UIButton *btnComp = (UIButton*)btn;
if(btnComp.tag == btnTapped.tag)
[btnComp setBackgroundImage:[UIImage imageNamed:#"selected_correct_answer.png" forState:UIControlStateNormal];
else
[btnComp setBackgroundImage:[UIImage imageNamed:#"default_image.png" forState:UIControlStateNormal];
}
}
}
Use -(void)custom:(id)sender instead of -(void)custom.
In the sender you can have the index of the button.
UIButton *answerBtn = (UIButton *)[self objectWihTag:1]