Select/Deselect for dynamically created UIButtons - ios

I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}

I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}

Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];

Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}

Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

Related

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

UIButton set title color not working

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

how to make UIView work as an scrollview

I need some serious help on this problem of mine where I need to work a UIView as an ScrollView. Below uploaded picture will clear all the doubts.
I have taken four buttons along with a UIView below them along with 4 container views which I have placed one above the other so as to work along with the four buttons.. What I want now is to work UIView as an ScrollView. Whenever I click any button, the View moves forward as well as the it changes the container view. And respective container view shows whenever I click a button.
Hi better you can use UIScrollView instead of UIView like:
//Declare it
NSMutableArray * buttonArray
int selectedIndex;
-(void)setUpSegmentBar {
NSArray * namesOfMenus =#[#"Button1",#"Button2",#"Button3",#"Button4",#"Button5"];
// Scrollview added on storyBoard
myScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 2, self.view.frame.size.width, 40)];
CGFloat scrollWidth = 0.f;
buttonArray=[[NSMutableArray alloc]init];
for ( int j=0; j<namesOfMenus.count; j++)
{
NSString * name =[namesOfMenus objectAtIndex:j];
CGSize size = [name sizeWithAttributes:
#{NSFontAttributeName: [UIFont fontWithName:font_bold size:font_size_button]}];
CGSize textSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
CGFloat strikeWidth;
if (iPAD) {
strikeWidth = self.view.frame.size.width/5.5;
}else{
strikeWidth = textSize.width;
}
CGRect frame = CGRectMake(scrollWidth, 0,strikeWidth+20, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:j];
button.frame = frame;
[button setBackgroundColor:[UIColor whiteColor]];
button.titleLabel.textColor=[UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderColor=[UIColor whiteColor].CGColor;
button.titleLabel.textAlignment=NSTextAlignmentCenter;
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:name forState:UIControlStateNormal];
// ScrollWidth As per Button Width
scrollWidth= scrollWidth+strikeWidth+20;
// to Check Which Button Is Selected.
if (j==selectedIndex) {
// If Selected Do UI Changes
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
// Other Buttons
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_disselected_Color;
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
// Add Buttons in Array
[buttonArray addObject:button];
// Add button on Scroll View
[myScrollView addSubview:button];
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 30.f);
myScrollView.pagingEnabled = NO;
myScrollView.backgroundColor=[UIColor whiteColor];
[myScrollView setShowsHorizontalScrollIndicator:NO];
[myScrollView setShowsVerticalScrollIndicator:NO];
return myScrollView;
}
And For Handeling button action.
-(void)buttonEvent:(UIButton*)sender
{
NSInteger index= sender.tag;
selectedIndex= (int) index;
for(int i=0;i<buttonArray.count;i++)
{
UIButton * button =(UIButton*)[buttonArray objectAtIndex:i];
if (i==selectedIndex) {
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
button.backgroundColor=segment_disselected_Color;
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
}
CGRect frame1 = myScrollView.frame;
UIButton * bt=(UIButton*)[buttonArray objectAtIndex:index];
frame1 =bt.frame ;
// By using it button will scroll and show properly
[myScrollView scrollRectToVisible:frame1 animated:YES];
[self setupTableAfterClick];
}

How to remove the button pragmatically with tag

I am adding button subView to each row I click. I am trying to remove the button subView every time once I click on different row and add button subview to the other row I click.
Where would be my issue? It is adding subView to each row without removing any of them.
if (sender.tag == 212)
{
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
UITableViewCell *cell = (id)sender.superview.superview.superview;
CGPoint center= sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:center toView:commonTable.table];
NSIndexPath *indexPath1 = [commonTable.table indexPathForRowAtPoint:rootViewPoint];
itemdata1 = [itemList objectAtIndex:indexPath1.row];
if (closeManager) [closeManager release];
CGFloat __y = commonTable.y + commonTable.table.y - commonTable.table.contentOffset.y + cell.height*indexPath1.row + sender.y;
CGFloat __x = commonTable.x + commonTable.table.x + sender.x;
CGRect layerBox1 = CGRectMake(618, __y, 90, sender.height+3);
CGRect layerBox2 = CGRectMake(888, __y, 94, sender.height+3);
NSLog(#"%f and %f", __x, __y);
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton2.frame = layerBox2;
[[myButton2 layer] setBorderWidth:2.0f];
[myButton2 addTarget:self action:#selector(func2:) forControlEvents:UIControlEventTouchUpInside];
myButton2.layer.borderColor = [UIColor redColor].CGColor;
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
}
Edited for Bhumeshwer katre:
if (myButton&&myButton2)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//Other variables
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = layerBox1;
[[myButton layer] setBorderWidth:2.0f];
myButton.layer.borderColor = [UIColor redColor].CGColor;
[myButton addTarget:self action:#selector(func1:) forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 666;
[self addSubview:myButton];
}
don't create each time your button like this
Here you are creating each time new instance of UIButton
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
And you are doing this
//Debug shows that every-time myButton&myButton2 hasn't tag??
if (myButton2.tag != 0 && myButton.tag != 0)
{
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
}
//It will just remove new instance of UIButton and no more your UIButton will be there on UIView.
try like this
if(!myButton){
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
if(!myButton2) {
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
//Don't remove your button just change frame
if you still want to remove buttons then first remove
[myButton removeFromSuperview];
[myButton2 removeFromSuperview];
//then create new instance of button
Try adding tags to buttons before adding them to subview.
So this:
[self addSubview:myButton];
[self addSubview:myButton2];
myButton.tag = myButton2.tag = 666;
should become like this:
myButton.tag = myButton2.tag = 666;
[self addSubview:myButton];
[self addSubview:myButton2];
Regards,
hris.to
If you use tableview cell, Use this following cases,
1) Set tag to button while creating tableViewCell.
2) Hide that button by cell.yourButton.hidden = YES
3) When you click on row, just note down that indexPath as self.selectedIndexPath = indexPath in didSelectRowAtIndexPath:
4) Now reload your tableView. Just add one condition in cellForRowAtIndexpath: as below.
if (indexPath.row == self.selectedIndexPath.row)
{
cell.yourButton.hidden = NO;
}
else
cell.yourButton.hidden = YES;
for (UIView *view in self.view.subView) { // instead of self.view you can use your main view
if ([view isKindOfClass:[UIButton class]] && view.tag == ?) {
UIButton *btn = (UIButton *)view;
[btn removeFromSuperview];
}
}
i think you have to set tag your buttons first,because default tag of every UIControl is 0;

Delete button on UIWebView

Im getting touch location coordinates from UIWebView and displays the button with coordinates. It's working with coordinates match. So, i need to increase the button. I didn't use Array for button. When i delete particular button from UIWebView it delete all the button. Shall i use NSMutableArray?. Here x & y is the touch point coordinates in float value
if(x && y){
NSLog(#"x is %f",x);
NSLog(#"y is %f",y);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"click" forState:UIControlStateNormal];
button1.frame = CGRectMake(x, y, 30.0, 20.0);
// [btnArray addObject:button1];
button1.tag = gTag;
gTag++;
[wbCont.scrollView addSubview:button1];
}
Delete the button:
-(void)recycle:(id)sender{
for(int i=1;i<gTag;i++){
[[wbCont.scrollView viewWithTag:i] removeFromSuperview];
}
-(void)click1:(id)sender{
UIButton *mainBtn = (UIButton *)sender;
int mainTag = mainBtn.tag;
txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];
txtview.font = [UIFont fontWithName:#"Helvetica" size:12];
txtview.font = [UIFont boldSystemFontOfSize:12];
txtview.backgroundColor = [UIColor whiteColor];
txtview.scrollEnabled = YES;
txtview.pagingEnabled = YES;
txtview.editable = YES;
txtview.tag = mainTag*1000;
for(int i=0; i<[textArray count];i++){
txtview.text=[textArray objectAtIndex:i];
}
[self.view addSubview:txtview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[txtview addSubview:button];
recyclebtn=[UIButton buttonWithType:UIButtonTypeCustom];
recyclebtn.tag = mainBtn.tag;
[recyclebtn addTarget:self action:#selector(recycle:) forControlEvents:UIControlEventTouchDown];
[recyclebtn setImage:[UIImage imageNamed:#"recycle.png"] forState:UIControlStateNormal];
recyclebtn.frame=CGRectMake(0, 10, 30, 30);
[txtview addSubview:recyclebtn];
}
-(void)recycle:(id)sender {
UIButton *btnTemp = (UIButton *)sender;
[[wbCont.scrollView viewWithTag:btnTemp.tag] removeFromSuperview];
int txtTag = btnTemp.tag*1000;
[[self.view viewWithTag: txtTag] removeFromSuperview];
}

Resources