Trying to disable button using button.enabled = false, but having no luck - iphone

I'm getting frustrated because I am failing to have any control over my bar button items or my UIToolbar. I am trying to disable a UIBarButtonItem, but it continues to respond to touch events. Here is what I have done, the code is so simple I don't know why it isn't working.
in my .h:
IBOutlet UIBarButtonItem *button;
#property (nonatomic,retain) IBOutlet UIBarButtonItem *button;
and in .m:
#synthesize button;
-(void)function{
button.enabled = false;
}
Am I doing something wrong with the viewcontroller delegate? I don't understand why I get no response. Thanks for your help.

This is correct:
button.enabled = false;
So are you sure that this method is actually being called? (And if this is your actual code, you have a method called 'function'?)

You might be able to achieve this with:
button.target = nil;
button.action = nil;
More:
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html

did you connect the button in the xib file to the outlet in the .h file? You may have set the target/IBAction, but you need to setup the reciprocal path. i.e. dragging from the File Owner, which is most likely the viewController, to the button. Then selecting the name of the IBOutlet in your .h file.
Good luck

Related

Why won't my UIButton work?

I'm trying to call Sharekit with a UIButton, but when I tap the button running on my device, nothing happens.
This should be the relevant code in my view controller's .h
#import "SHK.h"
#import "SHKFacebook.h"
#interface ViewController : UIViewController <CaptureManagerDelegate>
{
IBOutlet UIButton *upload;
}
#property (nonatomic, retain) IBOutlet UIButton *upload;
-(IBAction)buttonPressed:(id)sender;
And in my view controller's .m (using code from example here http://gazapps.com/wp/2011/07/17/basic-sharekit-customization)
#synthesize upload;
-(IBAction)buttonPressed:(id)sender {
SHKItem *item;
NSURL *url = [NSURL URLWithString:#"http://www.gazapps.com"];
item = [SHKItem URL:url title:#"Checkout this site"];
[SHKFacebook shareItem:item];
}
-(void)dealloc
{
[upload release];
}
Besides trying to use a UIButton instead of UIBarButton, I've followed Sharekit's install instructions to the letter. In my storyboard, I CTRL-dragged to the "first responder" and linked up my button to buttonPressed. I'm not getting any warnings or errors in xcode. But nothing happens when I tap the button.
You code seems fine. The problem is probably within the nib file. Check your connections again.
Are you doing anything else with your upload (UIButton) property? You don't need that Outlet at all, if you only want to trigger one IBAction.
You could put a breakpoint in your buttonPressed method, to see if the connections are setup correctly.
you should use SHKActionSheet
SHKActionSheet *actionSheet = [EDShareActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:self.navigationController.toolbar];

Objective C / iPhone - how to bind UISlider to code

I'm just starting with Objective C, but I'm already running into a problem:
When I did this in the past, I had no problem with it:
I made a new UISlider, clicked it, CTRL dragged a blue line to the code, and somehow I was then able to read it's values.
However, I am now running into the problem that I can't draw those lines anymore.
By messing around I managed to get buttons to work, but the slider just won't work.
my Mainwindow.xib has got the iSaturateViewController class,
iSaturateViewController.xib has got the whole interface in it.
How can I get the UISlider to work without being able to draw that line?
How I have it now:
(in iSaturateViewController.h)
#import <UIKit/UIKit.h>
#interface iSaturateViewController : UIViewController {
UISlider * satSlider;
UILabel * satLabel;
}
#property (nonatomic, retain) IBOutlet UISlider * satSlider;
#property (nonatomic, retain) UILabel * satLabel;
-(IBAction) changeSaturation:(id) sender;
#end
(in iSaturateViewController.m):
#synthesize satLabel,satSlider;
-(IBAction) changeSaturation:(id)sender {
satLabel.text = [[NSString alloc] initWithFormat:#"Saturation: %d ", (Float32)satSlider.value];
NSLog(#"%f", satSlider.value);
}
Ath the moment the NSLog shows: 0.00000000
Also, I've set the Label in the Identity properties to: satSlider (slider) and satLabel (label).
Just to clarify my goal:
I want the slider's (satSlider) value to show in the UILabel (satLabel) via the function changeSaturation.
How can I do this?
Add the method declaration:
-(IBAction) changeSaturation:(id)sender;
to the header file iSaturateViewController.h
Then, assign the Value Changed action of your UISlider to your changeSaturation method. Right click on the slider and drag the Value Changed to the File's Owner and connect them. It should work.
You can also try this:
-(IBAction)changeSaturation:(id)sender {
UISlider *slider = (UISlider *)sender;
satLabel.text = [NSString stringWithFormat:#"Saturation: %f", slider.value];
NSLog(#"Saturation: %f", slider.value);
}
Regarding UILabel - add IBOutlet and connect it in the Interface Builder. The interface should be like that:
#import <UIKit/UIKit.h>
#interface iSaturateViewController : UIViewController {
IBOutlet UISlider * satSlider;
IBOutlet UILabel * satLabel;
}
#property (nonatomic, retain) IBOutlet UISlider * satSlider;
#property (nonatomic, retain) IBOutlet UILabel * satLabel;
-(IBAction) changeSaturation:(id) sender;
#end
Let me know if it works.
Looks like the key thing you’re missing is that the satSlider outlet isn’t hooked up, so its value is nil. You connected the slider’s action to the -changeSaturation: method, but it sounds like you didn’t also connect the satSlider property to the slider itself. Sending a message (like value in this case) to nil doesn’t do anything, and returns 0, so 0 is the value you’re getting out of it. Hook up your outlet by Ctrl-dragging from the property in the code to the slider in your UI and it should work.
You need to edit your header and add the method declaration. Then in IB, associate the first responder for value changed to the UISlider. Also slider.value, you need to read article: Setting interval value for UISlider

UIBarButtonItem not showing up

I have the following code:
#interface PartyListViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
UIBarButtonItem * toggle;
//ProfileViewController *profileViewController;
}
#property(nonatomic, retain) IBOutlet UIBarButtonItem *toggle;
- (IBAction)toggleAction:(id)sender; // when the toggle button is clicked
#end
My interface builder looks like this:
I have added:
self.navigationItem.rightBarButtonItem = self.toggle;
in my viewDidLoad, why doesn't the button show up?
If I create my button via code, it is there...
Check if your toggle button is not nil when assigned it to the rightBarButtonItem property!
I always create my barButtonItem via code....
I am very curious why you add IBOutlet at both place. Generally, I will add this keyword at where I declare the property.
I declared it as a tableviewcontroller and it should be a viewcontroller
do you have
#synthesize toggle;
getting called in your implementation? If you don't synthesize the object, it won't make getters and setters for you, so that may be why setting the rightBarButtonItem isn't working, as there is no getter for the toggle barButtonItem, so you're setting it to nil

IBAction is not shown in IB?

i have done like
#interface tabViewController : UIViewController
{
IBOutlet UIButton* button_first;
IBOutlet UIButton* button_second;
IBOutlet UIButton* button_third;
IBOutlet UIButton* home;
}
but i could not in file owner those button? what i have to do? any help please? i have set everything correctly (viewcontroller, view)...the day before it was working...i tried in interface builder to reload classes etc...why it happens?
You should have this:
VC Header file:
#interface tabViewController : UIViewController
{
IBOutlet UIButton* button_first;
IBOutlet UIButton* button_second;
IBOutlet UIButton* button_third;
IBOutlet UIButton* home;
}
- (IBAction) ButtonMethod;
VC .m file:
- (IBAction) ButtonMethod
{
// Code goes here
}
You should now be able to link to the "ButtonMethod" in Interface Builder. Right click the button, click on the circle next to "Touch Up Inside" and link it to "ButtonMethod".
Hope that helps.
EDIT:
This images doesn't show the blue line that appears when selecting but shows what you have to press:
Did you declare them as properties?
Using:
in .h-fle
#property(nonatomic, retain) UIButton *button_first;
And in .m-file:
#synthesize button_first?
?
have a look at the nib file. may be you have changed the class of that nib file.
Check out whether you see tabViewController in fornt of File's owner.
I am sure you have changed the class so the change is not getting reflected of IBOUTLET in files owner.
and as told by #GUBB its not at all required to set hte property and synthesis of the stuffs..
hAPPY cODING...

How to trigger methods with UIButtonBarItem?

I have a a xib with a toolbar and on that I have 2 buttons all made in IB.
I can connect my outlets to the buttons but when I click them the method isn't triggered?
Why is that?
In my header file I have this:
#property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelEntry;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *saveEntry;
-(IBAction) cancelEntry:(id) sender;
-(IBAction) saveEntry:(id) sender;
In my .m file I got this:
-(IBAction) cancelEntry:(id) sender {
NSLog(#"cancel");
}
-(IBAction) saveEntry:(id) sender {
NSLog(#"save");
}
All compiles as it should but no nothing in the log when clicking the buttons.
What should I do to get it to work?
Sounds like you connected your outlets to the buttons, but didn't connect the buttons to the actions.
Right click Cancel the UIBarButtonItem in Interface Builder.
Hover over Selector, then drag the + that appears onto File's Owner.
Connect it to cancelEntry:.
Repeat for Save.
Instead of using (id) for your IBActions, you should probably use (UIBarButtonItem *). That'll prevent you from connecting a different object type to those IBActions.