Calling function with 1 parameter # selector. - objective-c

// 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

Related

pressing UIBarButtonItem in UINavBar gives unrecognized selector sent to instance

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:)];

ABPersonViewController in Modal

I would like to display ABPersonViewController as a modal instead of pushing it on the navigation stack. I've got this working but to keep a done button present I've had to use an NSTimer to add the button every 0.25 seconds because the done button may be removed when the view appears and is always removed when the app enters the forground. This is a pretty lame hack so I'm wondering if anyone has a better idea :)
I made a subclass of ABPersonViewController that adds the done button and starts the timer on view did load and invalidates it when the view is deallocated.
Here is what my code looks like to show the modal:
- (IBAction)showContactModal:(id)sender{
CNABPersonViewController *personViewController = [[CNABPersonViewController alloc] init];
personViewController.displayedPerson = self.contact.record;
personViewController.addressBook = [[CNAddressBookManager sharedManager] addressBook];
personViewController.viewDelegate = self;
personViewController.shouldShowLinkedPeople = YES;
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:personViewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
}
I had success in doing it like this. Insert this line to add a button to the navigation bar:
personViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Test" style:UIBarButtonItemStylePlain target:self action:#selector(_yourAddressBookAction)];
If this does not solve your problem, please show us the code that you had the issue with.

Have a textField input show as a label on a different view?

Like my question states. I want to have a textField and have a user type something in and press a button and it shows as a label on different view? What would I use to do. Could you give an example in code form. Plus it is going to be like 28 different TextFields?
Set a delegate for your text field. The textFieldDidEndEditing: method of the UITextFieldDelegate protocol then allows you to intercept the event that occurs once the text field has finished editing.
Your implementation of textFieldDidEndEditing: can then instantiate a UILabel (or some other type of view) that contains the text that the user input into the text field.
-(IBAction)clickButton:(id)sender{
if (!createViewController) {
createViewController = [[CreateViewController alloc] initWithNibName:#"CreateViewController" bundle:nil];
}
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:createViewController animated:YES];
}
and pass yourTextfield.text to CreateViewController and assign to yourLabel.text
I think it will be helpful to you.

UIBarButton Problem

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

How to change the text of UINavigationBar back button?

I want to ask a question about the iPhone application. I create the UINavigationController programmatically. And I use the UITableView to do the following thing. However, I don't know how to change the text of the text in the back button (see below, in this case is 'Plays') in code level? Thank you very much.
alt text http://www.freeimagehosting.net/image.php?02730817e4.png
Link: http://www.freeimagehosting.net/image.php?02730817e4.png
To customize the back button you modify the view controller you are going back to. So you can either set the title for your "Plays" view controller:
- (void)viewDidLoad {
// ...
[self setTitle:#"Whatever"];
}
Or access the back button item:
- (void)viewDidLoad {
// ...
// target/action must be nil
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Whatever"
style:UIBarButtonItemStyleBordered
target:nil action:nil] autorelease];
}
You have to actually change the text of the back button before pushing the new view controller onto the stack.Otherwise the back button text will not be displayed.
put this in you viewDidLoad, and i think you will get what you need.
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"yourTitle"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];