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];
}
Related
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.
Here there are n UIButtons and when selecting a particular UIButtons it is removed from SuperView. The remaining buttons frame's are moved to this point. Similarly trying to replace the tags of other buttons with selected buttons. This is what I have tried so far.
-(void)totesttheFunction
{
for(int i=0; i<7; i++)
{
UIButton *testHere = (UIButton*)[self.view viewWithTag:i];
if([testHere isSelected])
{
int backuptagFor = testHere.tag;
CGFloat diff = 30.0;
for(int j=i+1; j<7;j++)
{
UIButton *btnToReplace = (UIButton*)[self.view viewWithTag:j];
CGRect setRect = CGRectMake(btnToReplace.frame.origin.x-diff, btnToReplace.frame.origin.y, btnToReplace.frame.size.width, btnToReplace.frame.size.height);
btnToReplace.tag = backuptagFor;
[testHere removeFromSuperview];
}
}
}
}
Here integer variable difference is the difference in frames between two adjacent UIButtons.
I dont know for what purpose you are using this code.
But UICollectionView will be a better approach to solve your problem.
Add multiple buttons in collectionview cell .
then collection view will itself manage the deletion and index of each cell.
In my understanding the solution can be like below.
Adding buttons to view
for (int i=0; i<7; i++) {
UIButton *btn = [[UIButton alloc]init];
btn.tag=i;
CGRect frame=CGRectMake(0, i*40, 90, 37);
[btn addTarget:self action:#selector(btnDidPressed:) forControlEvents:UIControlEventTouchUpInside];
[btn setFrame:frame];
[btn setTitle:[NSString stringWithFormat:#"Button %d",i] forState:UIControlStateNormal];
[self.view addSubview:btn];
}
Removing the pressed button
-(IBAction)btnDidPressed:(id)sender
{
UIButton *btn =(UIButton *)sender;
[btn removeFromSuperview];
}
Also you can rearrange buttons with in view subviews or store buttons in an array. While pressed button remove selected one and redraw remain buttons.
Initialize an array that stores buttons. [array addObject:btn] in viewdidload method.
Then you can remove selected and replace the other buttons like below. btnDidPressed method:
UIButton *btn =(UIButton *)sender; //Pressed button
CGRect rect = btn.frame; //pressed button frame
CGRect temp ;
int tag =btn.tag; //pressed button tag
[btn removeFromSuperview]; //remove from view
[arr removeObjectAtIndex:tag]; //remove from array
for (int i =0; i<arr.count; i++) {
UIButton *btnNew = [arr objectAtIndex:i];
NSLog(#"tags old %d new %d",tag,btnNew.tag);
if (btnNew.tag>tag) { //be sure not to move previous buttons
temp = btnNew.frame;
[btnNew setFrame:rect];
btnNew.tag=i; //change tag
rect = temp;
[self.view addSubview:btnNew];
}
}
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);
//...
}
//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...
I am displaying a NSMutableArray items in UITableViewCell acting like gridview.There are custom UIbuttons in a cell.Now when a user click on one button I want it to get highlighted.But when I am doing it what is happening is when I click on a button its colour changes to red .But when i click on the next button its colour is also changed to red but the prevoius buttons colour is also red.I want the prevoius button to remain unhighlighted and the present button to be highlighted .How can I do it?
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CGRect rect = CGRectMake(18+80*j, yy,57, 40);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
[button setContentMode:UIViewContentModeCenter];
NSString *settitle=[NSString stringWithFormat:#"%#",item.title];
[button setTitle:settitle forState:UIControlStateNormal];
NSString *tagValue = [NSString stringWithFormat:#"%d%d", indexPath.section+1, i];
button.tag = [tagValue intValue];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[hlcell.contentView addSubview:button];
[button release];
}
-(IBAction)buttonPressed:(id)sender {
int tagId = [sender tag];
int divNum = 0;
if(tagId<100)
divNum=10;
else
divNum=100;
int section = [sender tag]/divNum;
section -=1;
int itemId = [sender tag]%divNum;
UIButton *button = (UIButton *)sender;
if(button.enabled==true)
{
button.backgroundColor=[UIColor redColor];
}
NSLog(#"…section = %d, item = %d", section, itemId);
NSMutableArray *sectionItems = [sections objectAtIndex:section];
Item *item = [sectionItems objectAtIndex:itemId];
NSLog(#"..item pressed…..%#, %#", item.title, item.link);
}
How can I do it?
one way to do it, is to keep an IBCollection reference for all your buttons in your header file (hook up all your buttons in Interface builder to this IBOutletCollection):
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *allOfMyButtons;
and in your buttonPressed method make the background color red only if its the currently pressed button AND that button is enabled (all others turn back to black or whatever the initial color was):
-(IBAction)buttonPressed:(id)sender {
[self.allOfMyButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIButton *button = (UIButton *)obj;
if (button != sender && button.enabled) {
[button setBackgroundColor:[UIColor redColor]];
} else {
[button setBackgroundColor:[UIColor blackColor]];
}
}];
}