i want to store the button in array - ios

I am creating a nine button using for loop, i want to store the button in an array because i want to access all button at a time.
for(i = 0; i < 9; i++ )
{
num1 = [UIButton buttonWithType:UIButtonTypeCustom];
[num1 setExclusiveTouch:YES];
[num1 setTag:tag_start+i];
num1.tag = currentTag;
currentTag++;
[num1 setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
[num1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[num1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[num1.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:35.0]];
//Store the button in array
[numBtnArray addObject:num1];
NSLog(#"%d", numBtnArray.count); //it show 0 nothing in that array
if(i >= 5) {
num1.frame = CGRectMake(430, i*60-160, 50, 50);
}
else {
num1.frame = CGRectMake(350, i*60+140, 50, 50);
}
[num1 addTarget:self action:#selector(buttonFunction:) forControlEvents:UIControlEventTouchUpInside];
[num1 setBackgroundImage:[UIImage imageNamed:#"greyBtn.png"] forState:UIControlStateNormal];
[num1 setBackgroundImage:[UIImage imageNamed:#"greyBtn.png"] forState:UIControlStateHighlighted];
[self.view addSubview:num1];
}

I don't understand what is the requirement but if you need to store them in an array simply do
NSMutableArray *btnArray = [NSMutableArray new];
for(i = 0; i < 9; i++ )
{
// ...
[btnArray addOjnect:num1];
[self.view addSubview:num1];
}

It's not clear exactly what you want, but I would encourage you not to store the buttons in an array at all, as the UIView already does that.
If you want to perform some sort of mass change to the buttons then you can, either:
Set a tag value in the button, so you can identify them later (with a minimum or maximum range).
Only target the buttons by testing their class.
So, if you want to change the background colour of the buttons in self.view, you simply:
for (id obj in self.view.subviews) {
if (![obj isKindOfClass:[UIButton class]])
continue;
UIButton *button = (UIButton *)obj;
button.backgroundColor = [UIColor blueColor];
}

Change the code as follows
numBtnArray = [[NSMutableArray alloc] init];
for(i = 0; i < 9; i++ ) {
UIButton *num1 = [UIButton buttonWithType:UIButtonTypeCustom];
//...
//Store the button in array
[numBtnArray addObject:num1];
NSLog(#"%d", numBtnArray.count);
//...
}

Related

How to change a image when when we click a button?

I'm working with radio buttons, for this i'm writing code like this.
float x = 40;
float y = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.radioButton.frame = CGRectMake(x, y+35, 35.0, 35.0);
[self.radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[self.radioButton addTarget:self action:#selector(radioBtnMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:self.radioButton];
self.radioButton.tag = [[self.radioBtnTagArr objectAtIndex:i] intValue];
y = y+70;
}
This "self.radioBtnTagArr" comes from server (tags comes from server), based on that count i want to create radio buttons and i'm assigning tag that tags to radio buttons.
- (void) radioBtnMethod:(UIButton *)sender {
for (int i=0; i<self.radioBtnTagArr.count; i++) {
if (sender.tag == [[self.radioBtnTagArr objectAtIndex:i] intValue]) {
[self.radioButton setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[self.radioButton setSelected:YES];
}
}
}
Now the problem is when i click first radio button the last radio button was selected like this below image...
I want only when we click first button first button state selected, if we click second button second button state selected like this...
do like create another one array for save the UIbutton, for e.g
Step-1
#property (nonatomic, strong) NSMutableArray *buttonArray;
step-2
add the all buttons to buttonArray
// allocate the memory for your array
buttonArray = [NSMutableArray array];
for (int i=0; i<self.radioBtnTagArr.count; i++) {
// create instance value of UIButton
UIButton *radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
radioButton.frame = CGRectMake(x, y+35, 35.0, 35.0);
[radioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
[radioButton addTarget:self action:#selector(radioBtnMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:self.radioButton];
y = y+70;
// add the object to array
[buttonArray addObject:radioButton];
}
step-3
inside the target
- (void) radioBtnMethod:(UIButton *)sender {
// reset the previous selection
for (UIButton *getradioButton in buttonArray) {
[getradioButton setImage:[UIImage imageNamed:#"radio_empty.png"] forState:UIControlStateNormal];
}
// set the current selection
[sender setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}

how to change button image in array list using button action?

-(void)createButton
{
int row = 0;
y = 340;
int column = 0;
for(int i = 0; i < myArray.count; ++i)
{
x=column*110+10;
for (y=340; y <= (x+ 90); y=y+320)
{
x=x-10;
}
NSString *imgPath = [myArray objectAtIndex:i];
NSString *imagePath = [newPath stringByAppendingPathComponent:imgPath];
myButton = [[UIButton alloc]initWithFrame:CGRectMake(x, row*110+35, 80, 80)];
myButton.tag = i;
UIImage *buttonImage =[UIImage imageWithContentsOfFile:imagePath];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// myImageView.backgroundColor = [UIColor orangeColor];
// myImageView.image =[UIImage imageWithContentsOfFile:imagePath];
[myScrollView addSubview:myButton];
if (row == 2)
{
row = 0;
column++;
}
else
{
row++;
}
}
[myScrollView setContentSize:CGSizeMake((column+1) * 110 + 10,372) ];
}
// in botton action
-(void)imageButtonAction:(UIButton *)sender
{
NSLog(#"sender tag is %i",sender.tag);
[myButton setImage:[UIImage imageNamed:#"close1.png"] forState:UIControlStateNormal];
}
but it change last button image of myArray
i can get tag number
You will always get last button the way you have because myButton is the last button that you have.
To get exact button with use of tag, use below code
UIButton *myButton = (UIButton *)[self.view viewWithTag:sender.tag];
Now you can set image to this button...
[myButton setImage:[UIImage imageNamed:#"close1.png"] forState:UIControlStateNormal];

How to select all buttons when we select last button in ios

Iam new to IOS programming,i have one doubt explained below:
I have 10 buttons as stars in the view,
If i click 10th button i need to show all buttons as selected.
If i click 5th button i need to show 5 buttons as selected.
I need this functionality to give rating in my ios app.
Please can anyone suggest me the solution for my question.
Thanks in advance
If I have to do this then I will give this button row an incremented tag from 1 to 10, then if someone press the 5 button i will get its tag and using loop change the button images from 1 to 5 getting the object of button using tag.
like you have selector for all button
-(void)userRatingAction:(id)sender {
UIButton* button = (UIButton*)sender;
for(int index = 1; index <= button.tag; index++) {
UIButton* ratingButton = (UIButton*)[self.view viewWithTag:index];
// set the button image here
}
}
I think you can find your answer with the help of below link http://try.sencha.com/touch/2.2.0/demos/Ext.List.select/
Use of Tag property can solve the problem.
Assign tag to all the UIButtons you have starting from 1-10. Create a common IBAction and on action..
-(IBAction)btnAction:(id)sender{
UIButton *selectedBtn=(UIButton *)sender;
//First Reset all the selection
for(UIButton *btn in self.view.subviews){
if([btn isKindOfClass:[UIButton class]]){
if(btn.tag>=1 && btn.tag<=10){
btn.selected=NO;
}
}
}
//Now Select
for(UIButton *btn in self.view.subviews){
if([btn isKindOfClass:[UIButton class]]){
if(btn.tag>=1 && btn.tag<=selectedBtn.tag){
btn.selected=YES;
}
}
}
}
Thats it.
Cheers.
To draw star buttons -
make imgArr global nsmutable array to store button objects
imgArr = [[NSMutableArray alloc ]init];
float leftMargin = 10;
float topMargin = 50;
for (int i=0; i<10; i++) {
UIButton *starBtn = [[UIButton alloc]initWithFrame:CGRectMake(leftMargin, topMargin, 25, 25)];
[starBtn setBackgroundColor:[UIColor clearColor]];
[starBtn setImage:[UIImage imageNamed:#"star_empty.png"] forState:UIControlStateNormal];
[starBtn setTag:i+1];
[starBtn setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[starBtn addTarget:self action:#selector(starBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:starBtn];
[imgArr addObject:starBtn];
leftMargin += 35;
}
-(void)starBtnClicked:(UIButton *)tempBtn{
NSLog(#"TEMP BTN CLICKED :: %#",tempBtn.titleLabel.text);
for (int i=0; i<imgArr.count; i++) {
UIButton *btn = (UIButton *)[imgArr objectAtIndex:i];
if(i < tempBtn.tag){
[btn setImage:[UIImage imageNamed:#"star_full.png"] forState:UIControlStateNormal];
}else{
[btn setImage:[UIImage imageNamed:#"star_empty.png"] forState:UIControlStateNormal];
}
}
}
to do so follow these steps.
Step 1 - From xib set tag of all buttons sequentially in incremented order. (e.g. 101, 102, 103, 104, 105, 106, 107, 108, 109, 110)
Step 2 - Write following code in your .m file.
-(IBAction)rattingsButtonTapped:(id)sender {
int currentTag = [sender tag];
for (int index = 101; index <= 110; index++) {
if ([[self.view viewWithTag:index] isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)[self.view viewWithTag:index];
if (button.tag <= currentTag) {
button.selected = YES;
} else {
button.selected = NO;
}
}
}
}
Step 3 - set IBAction - UITouchupInside to above methods.
you are all set, Compile and check code.

get tag of UIButton and those with smaller tags

I'm not understanding if what I'm trying to do is possible or not.
I am creating buttons in for loop:
CGRect rect2 = CGRectMake(50, 230, 40, 40);
for (int i = 0; i<5; i++) {
NSString *stringI = [NSString stringWithFormat:#"%d",i+1];
NSString *stringItouch = [NSString stringWithFormat:#"%dselected",i+1];
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:[UIImage imageNamed:stringI] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:stringItouch] forState:UIControlStateSelected];
[button addTarget:self action:#selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i+1;
button.frame = rect2;
rect2.origin.x = rect2.origin.x + 45;
[scrollView addSubview:button];
}
and after in method touchButton i get the tag of touched button
-(void)touchButton:(id)sender {
UIButton *buttonSender = sender;
buttonSender.selected = YES;
NSLog(#"button tag %#",buttonSender.tag);
for (int i = buttonSender.tag-1; i>0; i--) {
NSLog(#"int = %d",i);
//for example if buttonSender.tag is 4, in this way i have 3,2,1
}
}
in the last loop i want to select the buttons that have the tag less than that touched (in this case 3,2,1)
is it possible or not???
thanks everybody
All you need is viewWithTag: like so:
-(void)touchButton:(id)sender {
UIButton *buttonSender = sender;
buttonSender.selected = YES;
NSLog(#"button tag %#",buttonSender.tag);
for (int i = buttonSender.tag-1; i>0; i--) {
NSLog(#"int = %d",i);
//for example if buttonSender.tag is 4, in this way i have 3,2,1
/* Add this line */
UIButton *tempButton = (UIButton *)[scrollView viewWithTag:i];
}
}

how to get button title using for loop

//here take a array with letters
_englishArray = [[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
int i=0;
float x=5,y=13;
//add array elements as button title
for (i=0; i< [_englishArray count]; i++) {
button.tag=i+1;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
button.frame= CGRectMake(x, y, 29, 30);
button.titleLabel.textColor =[UIColor whiteColor];
x= x+31;
if (x==320 || x>310) {
x=5;
y=y+40;
}
[_keyboardImageView addSubview:button];
}
// and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code...
//here get the button title useing for loop
for (int j=0;j<=[_englishArray count]; j++) {
// here get button from image view by useing tag
butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"%#",butt.titleLabel.text);
}
it prints null not give button title............
Note:i am using button.currentitle also
how is it possible..why its displays null?
Since you set the title with setTitle:forState:, use titleForState: to get the title:
NSLog(#"title is %#", [butt titleForState:UIControlStateNormal]);
Of course this assumes that butt isn't nil. You need to assign the button's tag after you allocate the button:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i+1;
Make sure you define your button in for loop like
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
and than specify tag.
button.tag=i+1;
You have to define button in for loop instead of .h file.
And for get title text
for (int j=0;j<=[_englishArray count]; j++) {
UIButton *butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"%#",butt.titleLabel.text);
}
hope it will help you.
_englishArray = [[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
int i=0;
float x=5,y=13;
//add array elements as button title
for (i=0; i< [_englishArray count]; i++) {
button.tag=i+1;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame= CGRectMake(x, y, 29, 30);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
//button.titleLabel.textColor =[UIColor whiteColor];
[_keyboardImageView addSubview:button];
x= x+31;
if (x==320 || x>310) {
x=5;
y=y+40;
}
}
//here get the button title useing for loop
// and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code....
for (int j=0;j<=[_englishArray count]; j++) {
butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"Required Result is %#",butt.titleLabel.text);
}
Try it ,it will work...

Resources