I'm trying to disable a button that I add to my navigation controller bar. Here is how I added it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
What is the best way to enable/disable items like these? I've tried this code:
addButton.disabled = YES;
but it doesn't work of course. Any help would be appreciated. Thanks.
Edit: Should be addButton.enabled = YES;
Oops
If you define addButton in your header, and #synthesize it, then you will be able to use addButton.enabled = NO;, there is no "disabled" setter.
.h
#interface MyViewController {
UIBarButtonItem *addButton;
}
#property(nonatomic,retain) UIBarButtonItem *addButton;
#end
.m
#implementation MyViewController
#synthesize addButton;
-(void)viewDidLoad{
addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
-(void)DoSomething{
addButton.enabled = NO;
}
Related
I want to custom the backBarButtonItem in the navigationItem,
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
}
but it didn't work while I used the leftBarButtionItem can work.
I don't know ,can anybody give me an answer?
- (void)viewDidLoad
{
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *cancelNavButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissController)];
[self.navigationItem setLeftBarButtonItem:cancelNavButton];
}
- (void)dismissController
{
[self.navigationController popViewControllerAnimated:YES];
}
You shouldn't have to create the back button yourself if you are pushing your view controllers using the UINavigationController's pushViewController:animated: method. This will push the new ViewController on the NavigationControllers stack and give you an automatic back button with the title of the previous ViewController.
The reason your button doesn't work is the target: and action: parameters are nil you could bind them to the UINavigationController's popViewControllerAnimated: method to achieve the same action as the default back button.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)
];
[[self navigationItem] setBackBarButtonItem:back];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItem = back;
}
I have the following code:
UIBarButtonItem *promoteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(promoteToInstagram:)];
[promoteButton setTitle:#"Promote"];
self.navigationItem.rightBarButtonItem = promoteButton;
I am trying to create a UIBarButtonItem with a custom title. Why does the above says cancel instead of Promote?
Any idea on how to fix this?
Use the correct initializer, perhaps?
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
UIBarButtonItem btnSort = [[UIBarButtonItem alloc]initWithTitle:#"Descending" style:UIBarButtonItemStyleBordered target:self action:#selector(pressAscending)];
[[self navigationItem] setRightBarButtonItem:btnSort];
In iOS5 we have leftBarButtonItems that we can set to a navigation bar, how can I do this for pre-iOS 5? I basically have an array of UIBarButtonItem that I wanted to set the bar to.
You can build own bar, and add it as left button:
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:#selector(firstButtonAction:)];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:#selector(secondButtonAction:)];
UIToolbarTransparent *toolbar = [UIToolbarTransparent new];
[toolbar setFrame:CGRectMake(0,0, 140,44)];
[toolbar setItems:[NSArray arrayWithObjects:firstButton, secondButton, nil]];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
self.navigationItem.leftBarButtonItem = customBarButton;
UIToolbarTransparent
.h
#import <Foundation/Foundation.h>
#interface UIToolbarTransparent : UIToolbar {
}
.m
#import "UIToolbarTransparent.h"
#implementation UIToolbarTransparent
- (id)init {
if (self = [super init]) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.translucent=YES;
}
return self;
}
#end
samfisher is right, but you can just use a custom UIView that contains your UIButtons, than use that UIView as a (single) leftBasButtonItem
I have subclassed the UIToolbar to make it easier to implement a safari next, previous, done sort of thing.
It all worked fine when i was adding it directly (ie not subclassing) but now that i am, it crashes every time i click one of the buttons with
-[keyboardToolBar hideKeyboard:]: unrecognized selector sent to instance
This is the first time i have attempted to subclass something so im not sure if i have done something the wrong way.
Code for the subclass
#interface keyboardToolBar : UIToolbar {
UIToolbar *keyboard;
UIBarItem *previous;
UIBarItem *next;
UIBarItem *done;
}
#property (nonatomic, retain) UIToolbar *keyboard;
#property (nonatomic, retain) UIBarItem *previous;
#property (nonatomic, retain) UIBarItem *next;
#property (nonatomic, retain) UIBarItem *done;
-(void)previousField;
-(void)nextField;
-(void)hideKeyboard;
#end
#import "keyboardToolBar.h"
#implementation keyboardToolBar
#synthesize keyboard, previous, done, next;
-(UIToolbar*)initWithFrame:(CGRect)frame{
//Create a new toolbar
keyboard = [[UIToolbar alloc]initWithFrame:frame];
//Create all the buttons and point them to methods
UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:#"Previous"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(previousField:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(nextField:)];
UIBarButtonItem *filler = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(hideKeyboard:)];
//Set the width of both of the buttons to make it look pritty
previousButton.width = 70.0f;
nextButton.width = 70.0f;
self.previous = previousButton;
self.next = nextButton;
self.done = doneButton;
//Add the buttons to the toolbar
[keyboard setItems:[[[NSArray alloc] initWithObjects:self.previous, self.next, filler, self.done, nil] autorelease]];
//Release the buttons
[previous release];
[next release];
[filler release];
[done release];
//return the shiny new toolbar
return keyboard;
}
-(void)previousField{
}
-(void)nextField{
}
-(void)hideKeyboard{
NSLog(#"hello");
}
#end
and is called using UIToolbar *keyboard = [[keyboardToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
I have tried everything i can think of but still get the same error. Im sure i am just not retaining something somewhere of pointing the buttons to the wrong place but any help would be muchly appreciated
Thanks
Darc
The problem here is that your buttons are calling functions that take an input, but your functions do not take an input:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(hideKeyboard:)];
This means that there must be a method hideKeyboard:(id)sender. But you have
-(void)hideKeyboard{
NSLog(#"hello");
}
Either add the input to the function or remove the : from the selector call.
you effectively wrote it as a class method, here is a little bit better version, because of memory management issues etc.
-(UIToolbar*)initWithFrame:(CGRect)frame{
//Create a new toolbar
if ((self = [super initWithFrame:frame]))
//Create all the buttons and point them to methods
previous = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousField:)];
next = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextField:)];
UIBarButtonItem *filler = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
done = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(hideKeyboard:)];
//Set the width of both of the buttons to make it look pritty
previous.width = 70.0f;
next.width = 70.0f;
//Add the buttons to the toolbar
[keyboard setItems:[[[NSArray alloc] initWithObjects:previous, next, filler, done, nil] autorelease]];
//Release the buttons
[previous release];
[next release];
[filler release];
[done release];
//return the shiny new toolbar
return self;
}
also fix your methods to be either
#selector(someAction) to match -(void)someAction;
or #selector(someAction:) to match -(void)someAction:(id)sender;
also no reason to keep a reference to UIToolBar * keyboard, because you want that to be self.
actually probably no reason to keep references to the buttons at all unless you will need to change their titles or action/target pairs later.
You (or something you're doing) is calling hideKeyboard: (note the colon, indicating that the method has one argument.). However, the hideKeyboard method you've implemented, doesn't take any arguments.
Most likely, hideKeyboard should be changed to:
- (void)hideKeyboard:(id)sender {
NSLog(#"hello");
}
As an aside, the usual style for class names is capitalized, so your subclass should be KeyboardToolBar (and I'd also recommend keeping the same camel case as Apple's classes, so KeyboardToolbar would be best).
This doesn't seem to be working. What am i doing wrong?
-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(#"awaked");
[rightBarButtonItem release];
}
my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)
YourRootViewController *theRootController = [[YourRootViewController alloc] init];
UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}