How to select all buttons when we select last button in ios - 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.

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 make segmented control like this

Today I'm creating a custom segmented control. The appearance of the control as follow:
I tried two methods:
- (void)setBackgroundImage:(UIImage *)backgroundImage
forState:(UIControlState)state
barMetrics:(UIBarMetrics)barMetrics
- (void)setDividerImage:(UIImage *)dividerImage
forLeftSegmentState:(UIControlState)leftState
rightSegmentState:(UIControlState)rightState
barMetrics:(UIBarMetrics)barMetrics
However, here is the result I got:
I extracted the border image and the divider line (vertical line) from the design, but the result looks bad. Please help me if you have any idea.
I have always found it too much work to customize the UISegmentedControl and it is not really fully customizable.
I suggest you create yourself a custom UIView inheriting 3 buttons. They are fully customizable and fully controllable:
#define BASE_TAG 123
NSArray *titles = [NSArray arrayWithObjects:#"MAP", #"LOCATIONS", #"BUS", nil];
double y = 0.0;
double x = 0.0;
double width = (frame.size.width / [titles count]);
double height = frame.size.height;
for (int i = 0; i < [titles count]; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
[button setBackgroundColor:[UIColor clearColor]];
[button setTag:(BASE_TAG + ([titles count] - i - 1))];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[_buttons addObject:button];
x += width;
}
This is your button action. Here the button clicked will be selected and all others unselected:
- (void)buttonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
NSInteger index = 0;
for (UIButton *btn in self.subviews)
{
if (btn == button)
{
index = (btn.tag - BASE_TAG);
[btn setBackgroundColor:[UIColor colorFromHexString:#"#5ac8f5" alpha:1.0]];
[btn setSelected:YES];
}
else
{
[btn setBackgroundColor:[UIColor clearColor]];
[btn setSelected:NO];
}
}
/* Trigger a delegate here if you like
if ([_delegate respondsToSelector:#selector(didSelectedIndex:)])
{
[_delegate didSelectedIndex:index];
}
*/
}
This lets you pre set a selected index:
- (void)setSelectedIndex:(NSInteger)index
{
for (UIButton *button in self.subviews)
{
if (button.tag == (BASE_TAG + index))
{
[button setBackgroundColor:[UIColor colorFromHexString:#"#5ac8f5" alpha:1.0]];
[button setSelected:YES];
}
else
{
[button setBackgroundColor:[UIColor clearColor]];
[button setSelected:NO];
}
}
}
This is just a suggestion code I pulled from an old project. You will have to customize it for your needs.
Here is a very good article how to do custom segmented control.
Looks like you have tried similar approach but you have messed up something with images (probably you misunderstand the API so this article should do the job for you).

Replace tags of selected buttons to the remaining buttons

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 want to store the button in array

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);
//...
}

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