How to hide button after the button is manual added in Cocoa Touch? - objective-c

I have added button manually in for loop, after that, how to hide or change the button and hide label?
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:#"My Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UILabel *lblFileName = [[UILabel alloc] init];
lblFileName.text = [[objectArray objectAtIndex:i] valueForKey:#"fileName"];
lblFileName.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblFileName];
-(IBAction)myAction:(id)sender
{
// hide the button or change title button and hide label
}

If u are adding buttons & labels in for loop, then try following solution.
button.tag = i + 1;//0 is default tag for all views.
lblFileName.tag = i + 1;
-(IBAction)myAction:(id)sender
{
UIButton *btn= (UIButton *)sender;
[btn setHidden:YES];// hide the button
btn.titleLabel.text = [[objectArray objectAtIndex:[sender tag]] valueForKey:#"fileName"];
//Get the label of selected button using button tag.
[[self.view viewWithTag:[sender tag]] setHidden:YES];
}

-(IBAction)myAction:(id)sender
{
// hide the button or change title button
[button setHidden:YES];
}

In myAction:(id)sender sender is your Button.
- (IBAction)myAction:(id)sender {
[sender setHidden:YES];
}
But in this case you cannot make the button visible anymore, because you have no reference to it. I think it would be better to create a property for the button.

-(IBAction)btn:(id)sender
{
btn.hidden=YES;
}
try this.

Other answerers have already shown how sender is the button you want, butif you want to access the button in an different method (for example, if you want to make it visible again) you will need to create a property on the object that created the button.
To do so, put this in your class #interface:
#property (strong) UIButton *myButton;
And #synthesize it in your #implementation:
#implementation WhateverClass
#synthesize myButton = _myButton;
Then you can set the property to your custom button:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[self.myButton setTitle:#"My Button" forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.myButton];
and access it anywhere (after it has been created) like so:
-(IBAction)myAction:(id)sender
{
[self.myButton setTitle:#"Pressed!" forState:UIControlStateNormal];
[self.myButton setHidden:YES];
}

Give unique tag for each button and label like Girish said.No two tags should not be same.
Consider you have 10 button and 10 label. Then give tag for button 1 to 10 and label 11 to 20.
Then on your method you can access any label or button using corresponding tag.
UIButton *button=(UIButton *)[self.view viewWithTag:1];
UILabel *label=(UILabel *)[self.view viewWithTag:11];

Related

Program a UIButton to change UILabel

I am trying to use Objective-C in iOS Single View app to program a UIButton to change the text in a UILabel. Is there a way to run a function for this to be implemented in my "ViewController.m" file?
Here is my Source Code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView {
//UIBUTTON
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//set the view's background color
self.view.backgroundColor = [UIColor whiteColor];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(50, 280, 280, 50);
//set the font size of the button
button.titleLabel.font = [UIFont systemFontOfSize:40];
//set the text color of the button, before it is pressed
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//set the text color of the button, while it is pressed
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//set the button's title
[button setTitle:#"3" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[self.view addSubview:button];
//UILABEL
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
Question.text=#"How many horns does a Triceratops have?";
Question.backgroundColor = [UIColor grayColor];
Question.textColor = [UIColor whiteColor];
Question.font=[UIFont fontWithName:#"Helvetica" size:18 ];
[self.view addSubview:Question];
}
-(void)buttonPressed { }
#end
Is it possible to trigger the change in text in my buttonPressed function? Thanks so much for anybody's help!
The problem is that when you create your label and add it to the interface...
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
// ...
[self.view addSubview:Question];
...you didn't keep a reference to it. So now, in buttonPressed, you have no way to talk about the label.
Make a property:
#interface ViewController ()
#property (nonatomic, weak) UILabel* questionLabel;
#end
... and when you create the label, remember to set that property to that label. Now you will be able to access the text of that label later on, in buttonPressed (or anywhere else), through the property.

Button click not registering

I have an Xcode 5 button problem.
I create a number of button programatically on a UIViewController and then want to register clicks on these buttons. I use NSLog to track the clicks but it seems that no clicks are registered in the log.
Can anyone help please?
The ultimate aim to to segue to next UIViewController on each button click, but was just testing to see if button clicks were registered first.
Here is how I create the buttons in viewDidLoad (only 1 shown).
NOTE: the if-statement used only to check which buttons were pressed in previous view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *myString = [NSString stringWithFormat:#"%d", self.tagNumber ];
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:hotelButton];
// Add targets and actions
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
}
}
And here is how I check for button clicks
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
NOTE: I also tried the following:
- (void) buttonClicked:(UIButton *) button
{
NSLog(#"Button %ld clicked", (long int)[button tag]);
}
But that gave an error "undeclared selector buttonClicked" in my viewDidLoad pointing to action#selector):
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
Use this:
[hotelButton addTarget:self action:#selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
From Apple Docs:
UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.
UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
And keep this method:
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
try and change outside to inside,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Try this,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and pass the sender,
- (void) buttonClicked:(UIButton*)sender
{
NSLog(#"Button %i clicked", [sender tag]);
}
Have modified your if condition. Please follow :-
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:hotelButton];
}

UIButton show selected state on tap

How do I show the selected state for the UIButton on tap?
I want it so that when people tap on the uibutton it shows the selected state with the hover that I have?
Right now they have to hold the button to see the hover.
[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: #"hover.png"]] forState:UIControlStateSelected];
I want to show the hover image on tap also.
You need to set the selected state for the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:#"hover.png"] forState:UIControlStateSelected];
To show a hoover image while the button is pressed you need to set the state property to UIControlStateHighlighted
I had the same problem - my button was highlighted when pressed (tap+hold) and wasn't - when simply tapped. Here is my code:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image =[UIImage imageNamed:#"btn"];
UIImage *imageSelected =[UIImage imageNamed:#"btn_tap"];
[btn setBackgroundImage:transImage forState:UIControlStateNormal];
[btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
-(void)btnPressedWithDelay{
//your code for btn action
}
- (void) action:(UIButton *)sender{
[self performSelector:#selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}
Did it work for you?

iOS edit UIButton properties

I am working on the basics of iOS development.
So far I have a button that on press, will show some text. But what I want to do is after it is pressed, I want it to then change the text of the button, so far this is what I have:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:#"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)button_method:(UIButton *)sender {
NSString *test = #"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I try adding [button setTitle:#"You pressed the button"]; to button_method
This doesn't work... why? And how would I make it work?
When I try adding [button setTitle:#"You pressed the button"]; to button_method, it doesn't work... why?
Because that method is nonexistent on UIButton.
And how would I make it work?
By using a method that UIButton actually responds to. For example:
[sender setTitle:#"You pressed the button" forState:UIControlStateNormal];
And read the relevant documentation, please.
Your code will not change the title of the button. But simply add a label as a subview for your view. Is this what you want?
If you want to change the text on the button you will want to do sender.title.text = test; in your button_method
I also recommend adding a NSLog(#"Button pressed"); to your button press method to double check that it is being called.

Help with iphone button pressed

I am trying to see which button was clicked on so I can preform the correct logic.
This is the code for the buttons:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 423, 60, 60)];
[button addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[[UIImage imageNamed:#"refreshicon.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button.tag = 1;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 423, 60, 60)];
[button2 addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[[UIImage imageNamed:#"login.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button2.tag = 2;
[self.navigationController.view addSubview:button];
[self.navigationController.view addSubview:button2];
And this is how I call the buttonPressedAction:
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
if(button.tag == 1)
{
NSLog(#"1");
}else
{
NSLog(#"2");
}
}
But when I use NSLog to see what the sender value is, it crashes.
Any advice for what's happening and how to correct it?
Now corrected :o) THANKS!
As other have pointed out, UIButton does not have a value property. I guess you are trying to detect which button was clicked. Here are two ways to do this:
Use the tag property one each button. I.e. button1.tag = 1, button2.tag = 2. You can then test which button was clicked with if(sender.tag == 1) etc. You could introduce constants for the numbers to make the code more readable.
If you keep a reference to the button, you can check if the reference is equal. Example: if(sender == self.button1)
Probably the best approach would be to give buttons that do different things different actions, but failing that, you could distinguish them by tag.
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
// do something
}
If you were saving the pointers to the button objects you are creating, you could compare the pointers. Or you can just set the tag property for the button, and use that to switch behaviour.