passing a value to a button in xcode - ios

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

Related

IOS pass UIbuton to method as reference

In view.h I declare
#property (retain, strong) UIButton *btn;
In view.m I first have:
#synthesize btn;
and then in implementation I have this call to a method in another class:
[self.view addSubview:[otherclass getTestBtn:btn]];
After this line, in my view.m, I try to log the text in the button like this:
NSLog(#"btn.titleLabel.text = %#", btn.titleLabel.text);
Unfortunately the logs says:
btn.titleLabel.text = (null)
In the other class.m I implemented the method this way:
btn = [[UIButton alloc] initWithFrame:CGRectMake(70, 340, 100, 25)];
[btn setTitle:#"HELLO" forState:UIControlStateNormal];
return btn;
In the simulator I see that the button shows the text "Hello". But from view.m the text in the button don't seem to be accessible. Why?
Im afraid that the button that is passed from view.m to other class.m is not passed by reference. And hence the button in view.m is not affected by the method in other class. Is that the reason? If yes: how to pass the button as reference? I tried with & character. But the editor shows errors.
If you want to get the button returned from the other class then you need to assign it to your property -
self.btn = [otherClass getTestBtn];
[self.view addSubView:self.btn];
Also, you should strong rather than retain as #dimimpou said and you don't need #synthesize any more unless you want to use a specific backing variable name.

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.

ios - how to call the ShareKit share from an action button

I am working from this example of setting up ShareKit: http://getsharekit.com/install/
and their example has this:
- (void)myButtonHandlerAction
{
// Create the item to share (in this example, a url)
NSURL *url = [NSURL URLWithString:#"http://getsharekit.com"];
SHKItem *item = [SHKItem URL:url title:#"ShareKit is Awesome!"];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showFromToolbar:navigationController.toolbar];
}
But the last line there gives me a syntax error:
Use of undeclared identifier 'navigationController'
Also, my function is defined like this because I was looking to invoke the Sharing library after the user pressed the share button:
- (IBAction)share:(id)sender
Would anyone know why I am getting this error and what would be the right way to invoke the share functionality in my case?
Thanks!
Without seeing your entire code, it sounds like you don't have a UINavigationController, just a UIViewController. If that's the case, this should do the trick.
In you .h file
#property (nonatomic, strong) IBOutlet UIWindow *window;
#property (nonatomic, strong) UINavigationController *NVC;
In your .m file
UIViewController *theViewController = [[TheViewController alloc]initWithNibName:#"TheViewController" bundle:nil];
self.NVC = [[UINavigationController alloc] initWithRootViewController:theViewController];
[[self window] setRootViewController:NVC];
[self.window makeKeyAndVisible];

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

UIButton deallocated issue

I received this message from the debugger console :
-[UIButton release]: message sent to deallocated instance 0x1836b0
But I don't create UIButton programmatically, all my buttons have created in Interface Builder. Each of them are linked to a function, like this :
-(IBAction)theFunction:(UIButton *)sender;
In lot of this functions, I don't use the variable sender. I don't even try to release it. So I don't understand why my application try to release my buttons.
Do I do something in Interface Builder to release or not my UIButton? Is it about the picture I put in the UIButton? If I use the variable (UIButton *)sender, do I need to release it?
This problem stucks me because of it my application crashes.
Edit:
- (IBAction)showPopoverOverview:(UIButton *)sender {
TouchPlanePopover *content = [[TouchPlanePopover alloc] init];
[content setTheAlbum:#"Overview"];
// Setup the popover for use in the detail view.
detailViewPopover = [[UIPopoverController alloc] initWithContentViewController:content];
detailViewPopover.popoverContentSize = CGSizeMake(600., 400.);
detailViewPopover.delegate = self; // Set the sender to a UIButton.
// Present the popover from the button that was tapped in the detail view.
[detailViewPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// Set the last button tapped to the current button that was tapped.
lastTappedButton = sender;
[content release];
}
detailViewPopover is create in .h like this : #property (nonatomic, retain) UIPopoverController *detailViewPopover;
Thans for your help
Let me know if you need more information, I will edit the post
Change
-(IBAction)theFunction:(UIButton *)sender
to
-(IBAction)theFunction:(id)sender
Just try...
I am working on this and its work properly
So may be its useful for you.
myController.h
#import <\UIKit/UIKit.h>
#class myController;
#interface myController : UIViewController {
}
- (IBAction)onButtonClick:(UIButton *)button;
#end
myController.m
#implementation myController
- (IBAction)onButtonClick:(UIButton *)button {
NSLog(#"work properly");
}

Resources