change the text to uibutton in iOS - 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.

Related

iOS use of declared identifier in buttons

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

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.

passing a value to a button in xcode

I am seeing if there is a way to pass a variable to a button in xcode. I have a web link that I am retrieving from an API that I am storing to a NSString. I am just wondering if there is a way to pass that to a button so that when it is clicked it can follow the link accordingly.
You store that URL lcoally in some ivar or property. You assign an action to your button as usual. The action is nothing less than a method of your view controller. Within that method you execute that link.
This is how you can "follow" your link:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.klecker.de"]];
In your case you would use an NSString variable instead of the constant that I am using in this example.
You can just subclass the UIBUtton and add any properties/variables that you like/need. From your description I would use a NSURL instead of NSString...
Create a new UIButton subclass, in your .h file write:
#import <UIKit/UIKit.h>
#interface MyButton : UIButton
{
NSURL* buttonUrl;
}
#property (nonatomic, retain) NSURL* buttonUrl;
#end
and in the .m file simply:
#import "MyButton.h"
#implementation MyButton
#synthesize buttonUrl;
#end
Then in your ViewController: (don't forget to #import "MyButton.h)
MyButton *theButton = [[MyButton alloc] initWithFrame:someframe];
[theButton addTarget:self action:#selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
theButton.buttonUrl = [NSURL URLWithString:apiString];
[self.view addSubView:theButton];
[theButton release];
... And then you can get the URL again when the button was pushed as:
-(void)buttonPushed:(id)sender{
NSURL* theUrl = [(MyButton*)sender getButtonUrl];
}

IBAction Play an MP3

I have an App that I am making thatis similar to Urbanspoon. I have a picker the spins and it is hooked up to an IBAction for a UIButton. I want a mp3 file to play once the button is pressed. I feel it will give the user more of an experience by doing this.
I imported the AVFoundationFramework and the AudioToolBoxFramework and imported their .h into my main .m file
Here is my action.
- (IBAction)spinTheSpinner:(id)sender {
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"GoldTapButton.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"PurpleTapButton.png"] forState:UIControlStateHighlighted];
[sender setSelected:YES];}
[picker1 selectRow:(arc4random() % [self pickerView:picker1 numberOfRowsInComponent:0]) inComponent:0 animated:YES];
}
Do i need to make a separate action for the same button or can i just add a block of code into this same action. I have the mp3 file is my resources folder so its in my project I just cant find where and what to do to make it happen.
Here is my .h as well just for good measure
#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>
#interface ViewController : UIViewController
- (IBAction)spinTheSpinner:(id)sender;
#property (nonatomic, retain) NSMutableArray *picker1Data;
#end
also here is the top of my .m file
#import "ViewController.h"
#import "Twitter/Twitter.h"
#import "UIKit/UIKit.h"
#import"AVFoundation/AVAudioPlayer.h"
#interface ViewController ()
{
#private
UIPickerView *picker1;
}
#end
#implementation ViewController
Any help or suggestion or code would be a huge help in any way. I am running on a bit of a time schedule. THANKS S.O
Yes you can just add the code into the same IBAction method. You re not limited to th amount of code you can run in the method its currently linked to

Accessing UIButton from another class not working?

I am trying to access my UIButton in my Play class from my CCLayerClass.
The problem is that it is not working!
Here is how I declare it in the Play class:
.h
IBOutlet UIButton *pauseButton;
#property(nonatomic, retain) IBOutlet UIButton *pauseButton;
.m
#synthesize pauseButton;
Then in the dealloc:
[pauseButton release];
Also of course I connect it then in Interface builder.
Then in my other class (My CCLayer) class. I try to do this:
Play *play = [[[Play alloc] init] autorelease];
[play.pauseButton setHidden:YES];
The thing is, is that it simply just does not hide the button. Is there any reason for this?
Thanks!
Edit1:
My Play.h
IBOutlet UIButton *pauseButton;
BOOL pauseButtonVisible;
#property(nonatomic, retain) IBOutlet UIButton *pauseButton;
#property(readwrite) BOOL pauseButtonVisible;
.m
#synthesize pauseButton;
- (void)setPauseButtonVisible: (BOOL) variableToSet {
pauseButtonVisible = variableToSet;
if(pauseButton)
[pauseButton setHidden: !pauseButtonVisible];
}
- (BOOL) pauseButtonVisible
{
return(pauseButtonVisible);
}
viewWillAppear:
[pauseButton setHidden: !pauseButtonVisible];
I also made sure I connected it in Interface Builder
Then in CCLayerClass I do this:
Play *play = [[[Play alloc] init] autorelease];
if(play.pauseButton == NULL) {
NSLog( #"pause button is NULL");
}
But that NSLog gets called! Why is my pauseButton NULL? I just need to alloc it so it stays alive, is that possible?
Thanks!
play.pauseButtonVisible = YES;
Okay. Hopefully third time is the charm (and after that, I'm giving up cause it's time for me to go to bed).
Here in the .h file, I'm keeping the new pauseButtonVisible BOOL property.
#interface Play : UIViewController
{
BOOL pauseButtonVisible;
IBOutlet UIButton *pauseButton;
}
#property(nonatomic, retain) IBOutlet UIButton *pauseButton;
#property(readwrite) BOOL pauseButtonVisible;
#end
But in the .m file, we're doing something a little different:
#interface Play
// here we are rolling our own setters and getters
// instead of #synthesizing...
- (void)setPauseButtonVisible: (BOOL) variableToSet
{
pauseButtonVisible = variableToSet;
if(pauseButton)
[pauseButton setHidden: !pauseButtonVisible];
}
- (BOOL) pauseButtonVisible
{
return(pauseButtonVisible);
}
- (void) viewWillAppear: (BOOL) animated
{
[pauseButton setHidden: !pauseButtonVisible];
[super viewWillAppear: animated];
}
and
Play *play = [[[Play alloc] init] autorelease]; // you should really be using initWithNibName, but anyways
play.pauseButtonVisible = YES;
So now, hopefully pause button will be visible or hidden at the appropriate time for while your code is running.

Resources