Button click not registering - objective-c

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

Related

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

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

How does work viewWillAppear?

My viewWillAppear method call "-(void)doSomething".
- (void)doSomething
{
Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
if(delegate.booSomeValue == 0) {
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setFrame:CGRectMake(20,360,280,40)];
[aButton setTitle:#"Title"
forState:UIControlStateNormal];
[aButton addTarget:self
action:#selector(mySelector)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aButton];
}
}
It work, but aButton is still visible. What can I do to hide aButton? I have three UIViewController. In third i set delegate.booSomeValue to true. When I come back to previous UIViewController, I call this viewWillAppear but aButton is visible. I want to hide it.
The problem is that you added it once, and when you go back to it, you are not adding a second one, but the first one you added is still there, so you have to remove it.
To do so, you will need first to create a property to store the button, and check if it exists
if ( ... show button condition ... ) {
if (!aButton) {
... create and show button ...
}
}
else {
if (aButton) {
[aButton removeFromSuperview];
aButton = nil;
}
}
Move this code to viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad]
UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.tag = 101;
[aButton setFrame:CGRectMake(20,360,280,40)];
[aButton setTitle:#"Title"
forState:UIControlStateNormal];
[aButton addTarget:self
action:#selector(mySelector)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:aButton];
}
And
- (void)doSomething
{
Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
UIButton * aButton = (UIButton*)[self.view viewWithTag:101];
aButton.hidden = delegate.booSomeValue;
}

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.

How do I add resizing without IB

Since starting with iPhone app development (last 9 months) I have only used IB. I have a project to work on already built by another developer that I need to optimise for iPhone screen. No problem in IB, I know how to do that, in this project however the Nav bar is added using code only and is an image view. Could someone advise me how I go about resizing/positioning the nav bar when IB isnt used? Im trying to enhance this app for the iphone 5 screen.
#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)
#implementation ICNavbarView
#synthesize homeButton=__homeButton;
#synthesize prevButton=__prevButton;
#synthesize nextButton=__nextButton;
#synthesize delegate=__delegate;
- (id)initWithFrame:(CGRect)frame
{
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.9f;
// Add Navigation bar background // <<<<<< navigation bar from ui image
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
// Add back button
__prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
__prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
UIImage *prevButtonPressed = [UIImage imageNamed:#"BtnPrevPressed"];
[__prevButton setImage:[UIImage imageNamed:#"BtnPrev"] forState:UIControlStateNormal];
[__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
[__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
[__prevButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__prevButton];
// Add next button
__nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
__nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
UIImage *nextButtonPressed = [UIImage imageNamed:#"BtnNextPressed"];
[__nextButton setImage:[UIImage imageNamed:#"BtnNext"] forState:UIControlStateNormal];
[__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
[__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
[__nextButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__nextButton];
// Add home button
__homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
__homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
UIImage *homeButtonPressed = [UIImage imageNamed:#"BtnHomePressed"];
[__homeButton setImage:[UIImage imageNamed:#"BtnHome"] forState:UIControlStateNormal];
[__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
[__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
[__homeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__homeButton];
}
return self;
}
- (id)init
{
return [self initWithFrame:CGRectZero];
}
#pragma mark - Button handlers
- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
[self.delegate performSelector:#selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
[self.delegate performSelector:#selector(navBarHomeButtonPressed)];
} else {
[self.delegate performSelector:#selector(navBarNextButtonPressed)];
}
}
#end
So far I tried UIViewAutoresizingFlexibleHeight; like this
// Add Navigation bar background
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
//resize
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This seems to make no difference and the nav bar hasn't moved
I've used this before, but I used the setter function instead of using the property. I don't know if that would change anything or not, but try it this way:
[bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
The parens are important, if you have multiple re-sizing options.

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.