I am having one problem with UIBarButtons in xCode iOS 4 with Objective-C.
I am following several examples and the error says that the addButtonPressed method was not defined - even though I have the function created before hand like this:
- (void)addButtonPressed
{
NSLog(#"Addbutton pressed", #"");
}
It is also defined in the .h file. Here's my code:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:addButtonPressed];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
Here's the error:
'addButtonPressed' undeclared (first use in this function)
Am I doing something wrong?
Thanks for the help,
Christian Stewart
You should pass a selector for the action argument instead of the method name.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed)];
Related
I have a view that implements a splitView controller.
I had to add TWO buttons to the Navigation controller. I added the first in UIBuilder but it would not let me add a second one so I did it in code.....
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview: _splitViewController.view];
_splitViewController.delegate = _rightViewController;
_leftViewController.delegate = _rightViewController;
_rightViewController.leftView = _leftViewController;
bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
_navItem.leftBarButtonItem = bbi;
}
Where bbi was declared in the header as
#property (nonatomic, retain) UIBarButtonItem * bbi;
with the matching definition above and synthesis.
I have declared the done method as
- (void)done:(UIBarButtonItem *)sender;
but when I click the done button I get the error
-[ConfigurationViewController done]: unrecognized selector sent to instance
I have tried a number of things, with bbi declared locally instead of in the h file,
with done not being declared in the h file, having the done method expect an id instead, but no luck.
I am obviously doing something stupid. Any idea what?
I think done should have a colon after it if it is a method like this:
bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
Sup guys,
I have a tabBarController with a navigationController on each tab.
I wanted to set de default right bar button of navigation bar so I wouldn't have to write the same code on 3 different view controllers.
I tried [[UINavigationBar appearance] setRightBarButtonItem:myButton];
but had no success, is says:
-[_UIAppearance setRightBarButtonItem:]: unrecognized selector sent to instance
Then I tried to create my own UINavigationController subclass so I could set the button like:
self.navigationItem.rightBarButtonItem = myButton but again no success, nothing seems to happen probably because I dont have a navigationItem at this time.
Anyone have another solution?
If you inherit from the UIViewController you can set the right button like this
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"foobar.png"] style:UIBarButtonItemStylePlain target:self action:#selector(myAction)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
So I have a UIPopoverController what houses my UINavigationController where I have my UITableViewController however one of my options on the UITableView is to go and select an image with the UIImagePickerController... Now on the iPhone I can simply use presentModalViewController:animated: however calling that from within a UIPopoverController causes a crash so thats not possible...
I also know the UIImagePickerController needs its own UINavigationController so I can't just push pushViewController:animated: either...
So I figured out that if I keep a link to the UIPopoverController I created, I can then use setContentViewController:animated: to switch to the UIImagePickerController's viewController...
However, I am now stuck at giving the user a way to go back to the previous UINavigationController as I need to be able to add a cancel button to the UIImagePickerController but when I try to do this the cancel button won't get added...
Heres my code that i'm using
-(void)doPhotoalbums {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setContentSizeForViewInPopover:CGSizeMake(320, 480)];
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil];
[imagePicker.navigationItem setLeftBarButtonItem:cancel];
//[self presentModalViewController:imagePicker animated:YES];
[[self parentPopoverController] setContentViewController:imagePicker animated:YES];
} else {
[UIAlertView showMessage:#"This device does not have any photo albums."];
}
}
So my question is.. Does anybody know how I can get around this? either by adding a cancel/back button what I can hook up to make the navigationControllers switch back or another way to present this (i'd like to avoid switching between two UIPopoverControllers but I don't know what else I can do..
Thanks
Liam
Ahh.. after a little break I found this: https://discussions.apple.com/thread/1710435?start=0&tstart=0
using the UINavigationControllerDelegate you can use the navigationController:willShowViewController:animated: method to access the navigationBar.. then with some code (below) you can add a button.
if ([navigationController isKindOfClass:[UIImagePickerController class]]) {
UINavigationBar *bar = navigationController.navigationBar;
UINavigationItem *top = bar.topItem;
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(imagePickerControllerDidCancel:)];
[top setLeftBarButtonItem:cancel];
} else {
//do non imagePickerController things
}
I am programatically adding a UINavigationBar to a UIView, and now need to add a UIBarButtonItem to it. I am trying to use the following:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)];
[header setItems:[NSArray arrayWithObjects:doneButton, nil] animated:NO];
[doneButton release];
My app crashes and I find this in the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem setNavigationBar:]: unrecognized selector sent to instance 0x4b75c00'
Would appreciate it if someone could please point out what am I doing incorrectly here.
Thanks.
Ricky.
UINavigationBar accepts an array of UINavigationItem objects, each of which contain properties about a given level of the navigation hierarchy. You probably want to create a new UINavigationItem and then set its rightBarButtonItem property to your Done button.
It's unlikely you need to create a new UINavigationItem as the answer currently states. In contrast if you already have a UINavigationBar initialized from a nib which also contains a view, you can just add your UINavigationItem to the topItem property of your UINavigationBar. Something like this:
UIBarButtonItem *closeBtn = [[UIBarButtonItem alloc] initWithTitle:#"Close"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(closeBtnPressed)];
self.navigationBar.topItem.leftBarButtonItem = closeBtn;
[closeBtn release];
// Original //
I want to call this guy
-(void)addFavorite:(NSString *)favoriteToAdd
at, #selector here
action:#selector([addFavorite favoriteToAdd:#"string"])];
But I keep getting syntax error no matter which way I write it.
Can someone point out the appropriate way to call this function? When it had no parameter and was "addFavorite," it worked fine.
// Update //
I apologize for not being more specific. This is an iphone application. I have a view with a button, when the button is pressed, an NSString is grabbed and passed to addFavorite (function above). I get syntax errors when attempting to add a parameter to addFavorite.
I want to call the following addFavorite
-(void)addFavorite:(NSString *)favoriteToAdd
Something like this
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector([addFavorite: favoriteToAdd:#"testString"])];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
error: Expected ':' before '[' token
[obj action:#selector(addFavorite:) withObject:#"string"]
Edit: can't spell today :)
One of the methods for calling selector on button click:
[buttonObj target:self action:#selector(addFavoriteClick)]`
You'll then have to PULL the string in addFavoriteClick from where it's defined and pass it into addFavorite:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addFavoriteClick:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
This method will be called once the button got tapped
In the button click, pass the string which should be added as a parameter as follows
-(void)addFavoriteClick:(UIButton*)sender
{
NSString *str=#"stringtobeadded";
[self addFavorite :str];
}
This will help you