Get selected button value from the UIButton - ios

From the array I am creating button with the array values inside each button, Here I want to place a radio button before each button. while clicking the radio button i want to get it selected or not. In the below code I can display the button and the radio button image but cannot get the selected button value. How can i get the radio button selected or not for each button
//My Array
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three",#"four",#"five",#"six", nil];
//Creating button with placing a radio button image inside
-(void) createbutton:(CGRect) frame {
CGFloat xCoordinate = frame.origin.x;
CGFloat yCoordinate = frame.origin.y;
CGFloat buttonHeight = frame.size.height;
CGFloat buttonWidth = frame.size.width;
CGFloat gapBetweenTwoButton = 2;
for(int counter = 0; counter < [labelArrays count]; counter++) {
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button =[UIColor clearcolor];
[button setFrame:CGRectMake(xCoordinate,yCoordinate, 100, 40)];
NSString* titre = [labelArrays objectAtIndex:counter];
[button setImage:[UIImage imageNamed:#"radio_selected.png"] forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0,10,0,0);
[button setTitle:[NSString stringWithFormat:#"%#",titre] forState:UIControlStateNormal];
[button setUserInteractionEnabled:YES];
[self. button addTarget: self action: #selector(buttonAction:)forControlEvents: UIControlEventTouchUpInside];
[self addSubview: button];
if((counter+1)%3 == 0) {
xCoordinate = 0;
yCoordinate = yCoordinate + buttonHeight + gapBetweenTwobutton;
xCoordinate = xCoordinate + buttonWidth + gapBetweenTwoLabels;
}
}
//my frame
[self createbutton:CGRectMake(32,20,220,40)];

It's very simple to get selected button value by using its tag value. See here and try below code.
Put these line in viewDidLoad()
labelArrays = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", #"five", #"six", nil];
[self createButtons];
And then define these methods.
- (void)createButtons
{
int x = 30, y = 20;
for (int i = 0; i<labelArrays.count; i++)
{
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(x, y, 100, 40);
btnImage.backgroundColor = [UIColor redColor];
btnImage.tag = 700+i;
[btnImage setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btnImage.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[btnImage setTitle:[NSString stringWithFormat:#"%#", [labelArrays objectAtIndex:i]] forState:UIControlStateNormal];
[btnImage addTarget: self action: #selector(changeValue:)forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:btnImage];
if ((i+1)%2 == 0)
{
x = 30;
y = y+btnImage.frame.size.height+10;
}
else
{
x = x+btnImage.frame.size.width+30;
}
}
}
- (IBAction)changeValue:(UIButton *)sender
{
UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
if (btn.selected == TRUE)
{
[btn setImage:[UIImage imageNamed:#"radio_unselect.png"] forState:UIControlStateNormal];
btn.selected = FALSE;
}
else
{
[btn setImage:[UIImage imageNamed:#"radio_select.png"] forState:UIControlStateNormal];
btn.selected = TRUE;
NSLog(#"%#", btn.titleLabel.text);
}
}

Related

How to open and close tableView on a button click?

2I'm working on dropDowns, i'm creating buttons and dropdown tableView dynamically. When i click first button last tableView was opened. But i want to open specific tableView (at a time i want to open only one dropdown).
float y = 0;
float x = (self.answersView.frame.size.width/2)+175;
float Iy = 0;
float Ix = 675;
float Tx = (self.answersView.frame.size.width/2)+175;
float Ty = 0;
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue];
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
//The event handling method
- (void)dropdownBtnTapMethod:(UIButton *) sender {
if (self.dropdownTableView.hidden == YES) {
self.dropdownTableView.hidden = NO;
} else {
self.dropdownTableView.hidden = YES;
}
}
And how to set table view height dynamically.
Try this :
NSMutableArray *arrTableview; // make global
In ViewDidLoad
arrTableview = [NSMutableArray New];
Change in following method
for (int i=0; i<self.radioBtnTagArr.count; i++) {
self.dropdownBtn = [UIButton buttonWithType:UIButtonTypeSystem];
self.dropdownBtn.tag = i; // add this line
self.dropdownBtn.frame = CGRectMake(x, y+30, (self.answersView.frame.size.width/2)-200, 50);
[self.dropdownBtn setTitle:#"0" forState:UIControlStateNormal];
self.dropdownBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.dropdownBtn setTitleColor:[UIColor colorWithRed:24/255.0 green:56/255.0 blue:131/255.0 alpha:1] forState:UIControlStateNormal];
self.dropdownBtn.titleLabel.font = [UIFont systemFontOfSize:25];
[self.dropdownBtn addTarget:self action:#selector(dropdownBtnTapMethod:) forControlEvents:UIControlEventTouchUpInside];
self.dropdownBtn.backgroundColor = [UIColor lightGrayColor];
[self.scrollView addSubview:self.dropdownBtn];
//self.dropdownBtn.tag = [[self.btnTagArr objectAtIndex:i] intValue]; // This line commented.
y = y+70;
self.dropdownImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Ix, Iy+50, 20, 10)];
self.dropdownImageView.image = [UIImage imageNamed:#"dropdown.png"];
[self.answersView addSubview:self.dropdownImageView];
Iy = Iy+70;
self.dropdownTableView = [[UITableView alloc]initWithFrame:CGRectMake(Tx, Ty+80, (self.answersView.frame.size.width/2)-200, 50) style:UITableViewStylePlain];
self.content = #[ #"Monday", #"Tuesday", #"Wednesday",#"Thursday",#"Friday",#"Saturday",#"Sunday"];
self.dropdownTableView.delegate = self;
self.dropdownTableView.dataSource = self;
[self.answersView addSubview:self.dropdownTableView];
[arrTableview addObject :self.dropdownTableView];
Ty = Ty+70;
self.dropdownTableView.hidden = YES;
}
Selector
- (void)dropdownBtnTapMethod:(UIButton *) sender {
UITableView *tableView =[arrTableview objectAtIndex:sender.tag]
if (tableView.hidden == YES) {
tableView.hidden = NO;
} else {
tableView.hidden = YES;
}
}

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this
I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.
_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:#"%#",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;
[_selectednames addObject:_searchText.text];
for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];
}
now in my makeLabelsAndButtons funtion i done something like this
CGFloat xOffset = 10.0f;
int buttonCount = 0;
CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
NSString *nameString = [self.selectednames objectAtIndex:indexPath];
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {
xOffset = xOffset+10;
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
}
_button.frame = buttonFrame;
buttonCount++;
_button.tag = indexPath;
//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:#"Arial" size:8.0f];
_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;
[self.buttonView addSubview:_button];
NSLog(#"name: %#", nameString);
also on clicking button it will removed.any help would be thankful.
It works for me.Please try it.
y=20;
x=20;
for (int i=0; i<[arrAssignUsers count]; i++) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
CGFloat screenWidth=screenRect.size.width;
[arrBtnStatus addObject:[NSNumber numberWithBool:NO]];
NSString *strNames=[arrAssignUsers objectAtIndex:i];
stringsize=[strNames sizeWithAttributes:
#{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat m=x+stringsize.width+10;
CGFloat n=screenWidth-20;
if (m<=n) {
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width +10;
}
else
{
y=y+stringsize.height+10;
x=20;
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width+10;
}
[btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self
action:#selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor=WHITE_COLOR;
btn.layer.cornerRadius=10;
[btn.layer setMasksToBounds:YES];
btn.layer.borderWidth=2.0f;
btn.layer.borderColor=[UIColor orangeColor].CGColor;
[self checkNotifybtnStatus:btn];
[cell addSubview:btn];
}
}
It may helps to you.Thank you
You can use UICollectionView to create buttons. All you need to do is
1. Create custom cell with button as you want
2. When you tap on table view cell, add item to array and reload UICollectionView
3. Show buttons and it will automatically add scrollview for you.
When you tap on UICollectionView cell, remove object from array and reload UICollectionView again.
This will more easy as compared to adding buttons and setting frames.
And design UICollectionView cell in such way that it will same length as your text, use NSLayoutConstraints for it. It will work fast and smooth.
Code will be minimum and reusable.
Try out this working code...
Take UIScrollView in your Xib for buttons
//Adding menu buttons
btnArray = #[#"SYMPTOMATIC", #"SOCIAL", #"VOCATIONAL", #"PERSONAL"];
[self addButtonsInScrollMenu:btnArray];
/**
* Add Menu Buttons in Menu Scroll View
*
* #param buttonArray Array containing all menu button title
*/
#pragma mark - Add Menu Buttons in Menu Scroll View
-(void) addButtonsInScrollMenu:(NSArray *)buttonArray
{
CGFloat buttonHeight = self.menuScrollView.frame.size.height;
CGFloat cWidth = 0.0f;
for (int i = 0 ; i<buttonArray.count; i++)
{
NSString *tagTitle = [buttonArray objectAtIndex:i];
CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
[button setTitle:tagTitle forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Roboto-Regular" size:12.0f];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.menuScrollView addSubview:button];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
bottomView.backgroundColor = [UIColor darkTextColor];
bottomView.tag = 1001;
[button addSubview:bottomView];
if (i == 0)
{
button.selected = YES;
[bottomView setHidden:NO];
}
else
{
[bottomView setHidden:YES];
}
cWidth += buttonWidth;
}
NSLog(#"scroll menu width->%f",cWidth);
self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
}

Custom UIView subClass with UIScrollView

I am creating a class, that creates an option bar with multiple scroll views accessible by buttons.
This is how I add the subview with its content:
NSArray *scroll1 = [NSArray arrayWithObjects:#"normal",#"tiltshift",#"zoom",#"normal",#"blur",#"outline",#"coming soon", nil];
NSArray *scroll2 = [NSArray arrayWithObjects:#"Emoji Natur-01",#"Emoji Natur-02",#"Emoji Natur-03",#"Emoji Natur-04",#"Emoji Natur-05",#"Emoji Natur-06",#"Emoji Natur-07",#"Emoji Natur-08",#"Emoji Natur-09",#"Emoji Natur-10",#"Emoji Natur-11",#"Emoji Natur-12", nil];
NSArray *scroll3 = [NSArray arrayWithObjects:#"Linear",#"Parallel",#"Parallel 2",#"Crescendo",#"Dubstep",#"Blackhole",#"coming soon", nil];
NSArray *content = [NSArray arrayWithObjects:scroll1,scroll2,scroll3, nil];
[self.view addSubview:[self.bottomOptionView initWithFrame:self.view.frame withContent:content]];
The Subclass then generate a scrollview for each Array, with the buttons based on the content of the array:
-(void)addScrollViewOfContent:(NSArray*)array{
int viewNumber = array.count;
for (int i = 0 ; i < viewNumber; i++) {
//create the main buttons
NSArray *content = array[i];
UIButton *buttonAccess = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat buttonEdge = 40;
CGFloat buttonSpace = self.frame.size.width /viewNumber;
int placeOfButton = i+1;
CGFloat buttonAnchor = ((placeOfButton*(buttonSpace)) - (buttonSpace /2+ buttonEdge/2));
buttonAccess.frame = CGRectMake(buttonAnchor, 0 , buttonEdge, buttonEdge);
[buttonAccess setBackgroundColor:[UIColor redColor]];
buttonAccess.tag = i;
[buttonAccess addTarget:self action:#selector(buttonAccess:) forControlEvents:UIControlEventTouchDown];
[self addSubview:buttonAccess];
UIScrollView *ScrollView = [[UIScrollView alloc]init];
ScrollView.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height);
ScrollView.showsHorizontalScrollIndicator = YES;
ScrollView.tag = i;
ScrollView.backgroundColor =[UIColor colorWithRed:0 green:0.0 blue:0.0 alpha:0.0];
ScrollView.indicatorStyle = UIScrollViewDecelerationRateNormal ;
//UIImageView *selector = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"FILTER_SELECT.png"]];
CGFloat btnX = 0;
for (int i = 0 ; i < content.count; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(btnX, 2 , 65, 65);
UIImage *img = [UIImage imageNamed:[content objectAtIndex:i]];
[button setBackgroundImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(subButtonTarget:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:[content objectAtIndex:i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"MyriadPro-Cond" size:15];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.textAlignment = UIBaselineAdjustmentAlignBaselines ;
button.showsTouchWhenHighlighted = YES;
[ScrollView addSubview:button];
btnX = btnX + 100;
}
btnX = btnX - 80;
ScrollView.contentSize = CGSizeMake(btnX + 50, 80);
ScrollView.hidden = YES;
[self.scrollViewArray addObject:ScrollView];
[self addSubview:ScrollView];
}
This gives me exactly what I want.
Then I detect which button, of which scroll view is pressed to trigger the action.
-(void)subButtonTarget:(UIButton *)sender{
int button = sender.tag;
int scrollview = self.selectedScrollView;
[self.videoEditor subAction:button ofScrollView:scrollview];
}
-(void)buttonAccess:(UIButton *)sender{
// self.selectedScrollView = sender.tag;
for (int i=0; i< self.scrollViewArray.count; i++) {
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:i];
scrollview.hidden= YES;
}
int scrollIndex = sender.tag;
UIScrollView *scrollview= [self.scrollViewArray objectAtIndex:scrollIndex];
scrollview.hidden = NO;
}
Basically when one of the button is touched, it calls a function in the view controller in which the sublclass was initially called.
The problem is that, on touchevent nothing happens, except for NSLog. Would that be because of the dependency relation of the subClass and the VC that uses it?
You should set this one for each UIScrollView
ScrollView.delaysContentTouches = NO;
And try to subclass UIScrollView with override method
-(BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}

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];

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];
}
}

Resources