IBOutlet returning nil after IBAction Implementation (IOS) - ios

I have 3 UIButton created through the Interface Builder of Xcode.
Have the IBOutlet and IBAction defined in the Controller.h like this:
#property (strong, nonatomic) IBOutlet UIButton *btnToday;
#property (strong, nonatomic) IBOutlet UIButton *btnToday_less_1;
#property (strong, nonatomic) IBOutlet UIButton *btnToday_less_2;
- (IBAction) setBtnToday: (UIButton *) sender;
- (IBAction) setBtnToday_less_1: (UIButton *) sender;
- (IBAction) setBtnToday_less_2: (UIButton *) sender;
In my Controller.m have this:
#synthesize btnToday;
#synthesize btnToday_less_1;
#synthesize btnToday_less_2;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[btnToday setTitle:#"Today" forState: UIControlStateNormal];
[btnToday_less_1 setTitle:#"Yesterday" forState: UIControlStateNormal];
[btnToday_less_2 setTitle:#"Day before yesterday" forState: UIControlStateNormal];
}
The above code works OK, but if I implement the IBAction it does not.
The implementation looks like this:
-(IBAction)setBtnToday:(UIButton *)sender{ /* a method call */ }
-(IBAction)setBtnToday_less_1:(UIButton *)sender{ /* a method call */ }
-(IBAction)setBtnToday_less_2:(UIButton *)sender{ /* a method call */ }
If I do a NSLog of any of the IBOutlet they return nil. If remove the code of the IBAction from the Controller.m file it starts working again.
What am i doing wrong?
note: does not work means it compile without Errors and run but the text I am trying set on the UIButton are not updated.

You can't override setBtnToday:, setBtnToday_less_1:, and setBtnToday_less_2: like that because those are exactly the setter methods that the runtime is relying on to set up the buttons you configured in your xib file.
When you create a (non readonly) property the compiler synthesizes both a "getter" and "setter" method for your property. By default the getter method is - (<property type>)<property name> and the setter is - (void)set<CamelCase property name>. And those IBAction methods you posted are exactly the setters for your three button properties. By writing them explicitly (and making them do nothing) you're preventing the compiler from synthesizing the default setter implementations and that in turn prevents the xib loading code from working in the expected way.
If you're trying to implement methods to respond to the buttons being tapped you should just name them something else, like - (IBAction)btnTodayTapped:(id)sender, - (IBAction)btnToday_less_1_tapped:(id)sender, etc. Alternatively you could make one method, - (IBAction)buttonTapped:(id)sender and have that method inspect the sender to determine which button was tapped.

Related

textViewDidBeginEditing not working in IOS 9

i'm new in Objective C . What i'm going to do is simple just to animate keyboard up when textfield is focused and shrink when it's not. I already followed some other tutorial on how to set up this step by step but it not working.The event textViewDidBeginEditing is never get called when textfeld is focus.
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *cCodeTextField;
#property (weak, nonatomic) IBOutlet UITextField *jidTextField;
- (IBAction)nextBtnTapped:(id)sender;
#end
I'm also set the delegate to the textfield but it's not working.
- (void)viewDidLoad {
[super viewDidLoad];
[self addTextFieldBorder:_jidTextField];
[self addTextFieldBorder:_cCodeTextField];
_jidTextField.delegate = self;
}
-(void) textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"Enter");
}
It looks like your ViewController class is implementing methods from the wrong delegate. It is implementing UITextViewDelegate methods when it should be implementing UITextFieldDelegate methods.
Note that 'jidTextField' is of type UITextField.
Your delegate method is called 'textViewDidBeginEditing' which is a UITextViewDelegate method and takes a UITextView as a parameter.
The issue here is that your class is implementing delegate functions for UITextViewDelegate and not UITextFieldDelegate.
The correct method definition is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Here is a link to the documentation for the correct delegate:
https://developer.apple.com/reference/uikit/uitextfielddelegate
Hope this helps!

SetHidden not working

I am trying to show a label for some seconds when i press a button. But the hide function is not working properly.
-(void) hide_label:(NSString *)value{
[value setHidden:YES];
}
Get the error: No Visible #interface for 'NSString' declares the selector 'setHidden:'.
In your example, value is an NSString, not the UILabel. NSString's have no setHidden: method, as the error message suggests.
Instead, you will want to pass in the label itself and then call setHidden:.
So, change the method to:
- (void) hide_label:(UILabel *)label {
[label setHidden:YES];
}
And change all parts of the code that call this method to pass in the UILabel.
NSString is not a subclass of UILabel and does not respond to setHidden: you must call that on the UILabel property itself.
How do you declare your UILabel? It should be something similar to the following if you connect via a nib:
#property (nonatomic, weak) IBOutlet UILabel *label;
or if its created programatically:
#property (nonatomic, strong) UILabel *label;
You can then call setHidden on the label, as it is a property you can use dot syntax:
label.hidden = YES;
You can check its state by using its accessor:
if ([label isHidden]) {
//... do something
}
It might be worth you reading some tutorials on iOS development, Look Here on raywenderlich.com

UIButton enable doesn't work

I have a UIViewController with a single button and an activity indicator.
In the class for this VC MainViewController.m I do the following in viewDidAppear:
- (void)viewDidLoad
{
[super viewDidLoad];
_actLoadLoc.color = [UIColor blueColor];
_startButton.enabled = NO;
[_startButton setTitle:#"Fetching Location" forState:UIControlStateDisabled];
}
Another method in my MainViewController.m is called readyToGo and is implemented as follows:
-(void) readyToGo
{
[NSThread sleepForTimeInterval:1.0f];
NSLog(#"Done sleeping");
_startButton.enabled = YES;
[_startButton setTitle:#"Start" forState:UIControlStateNormal];
_actLoadLoc.stopAnimating;
}
I have properties for both UIButton, UIActivityIndicatorView and a declaration of the readyToGo method in my MainViewController.h as follows:
#property (weak, nonatomic) IBOutlet UIButton *startButton;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *actLoadLoc;
-(void) readyToGo;
The readyToGo method is called from another class abc.[h/m] which imports MainViewController.h. The call happens after one of the functions in abc.m completes filling an array with calculated data.
The call works since Done Sleeping shows in the output, however the startButton is not enabled, its test does not change and the actLoadLoc does not stop animating... Any idea what's wrong with my code/method?
Thanks in Advance!
You are calling the readyToGo on the wrong instance of the view controller. You have an instance which is displaying content on the screen and you are, in some way, creating a new one to call the method on. You need to get the existing one instead.
It's not ideal, but you should be able to get the controller with:
UINavigationController *n = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
SDPPMainViewController *mvc = (SDPPMainViewController *)[n viewControllers][0];
(Will need to add some casts, and should probably break out to multiple lines)

UIControlEventTouchCancel not triggering the function

I have two buttons in my xib file. This is my interface file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *testButtonOut;
#property (strong, nonatomic) IBOutlet UIButton *button2;
- (IBAction)testButton:(id)sender;
- (void)buttonReleased;
#end
These are the implementations of two functions in my implementation file
- (IBAction)testButton:(id)sender {
button2.highlighted=YES;
[testButtonOut addTarget:self action:#selector(buttonReleased) forControlEvents:UIControlEventTouchCancel];
}
- (void)buttonReleased{
button2.highlighted=NO;
}
When I click on the testButton and release, it does not trigger the buttonReleased function (I used breakpoint to check it), but if I change UIControlEventTouchCancel to UIControlEventTouchUpInside it does trigger the buttonReleased function when I click on it but I dont want to trigger it on button click I want to trigger it on button release, I also tried with TouchUpOutside but that didnt trigger as well. Which event should I use so that it calls the buttonReleased function when I release the click from the button event?
Instead of
"UIControlEventTouchCancel" use
"UIControlEventTouchUpInside."

change the text to uibutton in iOS

I have a mistake with change the text to uibutton in iOS
#interface ViewControllerSonidos : UIViewController{
__weak IBOutlet UIButton *initgame;
}
#property (weak, nonatomic) IBOutlet UIButton *initgame;
- (IBAction)Reproducir:(id)sender;
And my implementation file looks like this:
#implementation ViewControllerSonidos
#synthesize initgame
- (IBAction)Reproducir:(id)sender {
[initgame setTitle:#"Continue" forState:UIControlStateNormal];
}
but the problem is that the text is never changed, somebody looks what is wrong?
Thanks in advance!
You have many ways you can do this.
You can just connect it with the IBOutlet,
you can use it with id sender
or you could just use the simple IBAction signature
examples:
// using the id sender method
- (IBAction) buttonTitleChange:(id)sender {
[sender setTitle: #"Title" forState:UIControlStateNormal];
}
// using the connection one
- (IBAction) buttonTitleChange: (id)sender {
//AFTER CONNECTING THE BUTTON IN .XIB
[buttonName setTitle:#"Title" forState:UIControlStateNormal];
}
// and the regular void method....also requiring the linkage of the .xib
- (IBAction) buttonTitleChange {
[buttonName setTitle:#"Title" forState:UIControlStateNormal];
}
just remember to connect the button in .xib for them all
using the id sender, just says that you can use it for any type object. the id sender with connection is just a way of writing the method, and the regular method is just a way of practice.
It all depends on how you program.
Hope this helps!
try this code:
- (IBAction)Reproducir:(id)sender {
[sender setTitle:#"Continue" forState:UIControlStateNormal];
}
May be problem with IBOutlet ,you forgot to connect it in XIB otherwise it'l works fine.
-(void)btnChanged
{
[m_btnSample setTitle:#"Continue" forState:UIControlStateNormal];
}
Yes please check IBOutlet connection, because same code worked for me.
in ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)initgame:(id)sender;
#property (retain, nonatomic) IBOutlet UIButton *initgame;
#end
in ViewController.m
- (IBAction)initgame:(id)sender {
[self.initgame setTitle:#"Continue" forState:UIControlStateNormal];
}
don't forget to attach button from .xib to property and IBAction.
if It doesn't work now, just remove the connection between the Button and IBAction/Property, create new IBAction and property for it and it will work for the above code.

Resources