iOS use of declared identifier in buttons - ios

I'm a very new to iOS development but I have dabbled in Java and C before.
I'm trying to create a simple timer application and when the user presses 'start', the button text will turn into 'reset' but the compiler throws me a "Use of undeclared identifier 'btnStart'"
I've taken out the rest of the code because everything worked run until I tried to change the button text.
I'm pretty sure its correctly declared in the .h file and I'm thinking it might have to do with adding another #property argument for the button itself but that didn't really work. How do I properly declare a button then?
Thanks
my ViewController.m
- (IBAction)btnStart:(id)sender {
[btnStart setTitle: #"RESET" forState: UIControlStateNormal]; //Error shown here
}
my ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *lblDisplay;
- (IBAction)btnStart:(id)sender;
- (IBAction)btnStop:(id)sender;
#end

The sender should be the button itself. Try this
- (IBAction)btnStart:(UIButton *)sender {
[sender setTitle: #"RESET" forState: UIControlStateNormal];
}

Please declare button IBOutlet in .h or .m file where you have declared --
#property (weak, nonatomic) IBOutlet UILabel *lblDisplay;
#property (weak, nonatomic) IBOutlet UIButton *btnStart;
and use it like this
[self.btnStart setTitle: #"RESET" forState: UIControlStateNormal];

You can set title of the button for two states in viewDidLoad function like this
[btnStart setTitle: #"RESET" forState: UIControlStateNormal];
[btnStart setTitle: #"Start" forState: UIControlStateSelected];
and in your function use this code
- (IBAction)btnStart:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setSelected:!btn.isSelected];
}

Related

how to add target for button in subViewController of controller

a have 2 controller
#interface PlayVideoController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *playButton;
#property (strong, nonatomic) IBOutlet UIButton *exportButton;
- (IBAction)actionPlay:(id)sender;
and
#interface HomeController : CommonVideoViewController
in Homecontroller have 2 method
- (void)selectedVideoComplete:(NSDictionary *)infoVideo
playController = [self.storyboard instantiateViewControllerWithIdentifier:#"PlayVideoController"];
// playController = [[PlayVideoController alloc]initWithNibName:#"View" bundle:nil];
// [playController.playtButton addTarget:playController action:#selector(playAction) forControlEvents:UIControlEventTouchUpInside];
playController.urlMovie = [infoVideo objectForKey:UIImagePickerControllerMediaURL];
[self.view addSubview:playController.view];
NSLog(#"_____ %#",playController.playButton);
[playController.view addSubview:btn];
- (void)playAction {
[playController.player play];
}
why this code does not work
[playController.playtButton addTarget:playController action:#selector(playAction) forControlEvents:UIControlEventTouchUpInside];
i am test using storyboard and xib file
thanks
Currently you are implementing the playAction method in your HomeController, and it needs to be implemented in you PlayVideoController.
Or if your playAction method is only doing what your code shows it as doing, just set the action as follows:
[playController.playButton addTarget:playController.player action:#selector(play) forControlEvents:UIControlEventTouchUpInside];
Hope this helps!

Can't create IBOutlet for UIBarButtonItem in a UIViewController

I'm trying to create an IBoutlet from a UIBarButtonItem to a UIViewController, but when ctrl+clicking and dragging to the controller, I see no indicator whatsoever, and thus it's impossible to create it. Not sure what I'm doing wrong.
Here's the screenshot, in case it could clear something up:
Thanks a lot in advance!
EDIT: I also tried to create the IBOutlet manually with #property (weak,nonatomic) IBOutlet UIBarButtonItem *barButtonItem; and then ctrl+clicking and dragging, but same result.
I was able to create a UIBarButtonItem IBOutlet
#property (nonatomic, weak) IBOutlet UIBarButtonItem *cameraLibraryToggleButton;
if u used story board then select UIBarButtonItem item and just put on the view controller.then, set IBOutlet. try this
I do not think you can create an IBOutlet for a UIBarButtonItem... Instead create a IBOutlet for your UIToolbar and give your UIBarButtonItem a tag of 1. Next use this code:
for(UIBarButtonItem *button in self.toolBar.items)
{
if(button.tag == 1)
{
UIBarButtonItem *newButton = button; //Your UIBarButton
//Do whatever you want with it.
}
}
Make Sure you have assign class (e.g .UIViewController) to your storyBoard

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.

IBOutlet returning nil after IBAction Implementation (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.

Resources