Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give barbuttonitem action?

Tags:

ios

I want to bring up the .nib of "TableViewController" when a done button is clicked on my UIToolBar. But the below isn't allowing the click to bring up a new view. How do I rectify this? Please show me where I went wrong and what should be replaced and why.

//Here's the selector in my overlay. 
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)];

//Here's how I made my action. Btw, the uitoolbar has no nib, it's an overlay on the
//(camera mode).

-(void)doneButtonPressed {
TableViewController *tableView = [[TableViewController alloc]
initWithNibName:@"TableViewController" bundle:nil];
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tableView animated:YES];
}


//Yet nothing happens when I click on my done button on my overlay. And I've made sure
//   i've imported .h frameworks correctly too.

Suppose you were to bring up a nib from a barbuttonitem which is on a UItoolbar overlay. How would you do it?

I was told that to make it function properly I would have to add [barButtonItem addTarget:self action:@selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside]; .

But if I add it I get this:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone addTarget:self action:@selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];

Which results in me getting an error reading "instance method' - initWithBarButtonSystemItem:target:action:forControlEvents:' not found (return type defaults to 'id')"

Instead of showing me only the correct additive, please show me the solution in addition to the code I've written here.

like image 262
Capricorn Avatar asked Aug 01 '12 18:08

Capricorn


3 Answers

If you are using XCode 4, you can simply Ctrl+Drag the BarButtonItem, to your .h file, and you can create an IB Action from there automatically.

like image 177
chaitanya.varanasi Avatar answered Nov 15 '22 07:11

chaitanya.varanasi


A UIBarButtonItem follows the default UIControlEventTouchUpInside and you cannot set it. Xcode should auto-suggest the methods for allocating it, but the correct code is:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)];

Note, there's no forControlEvents:.

like image 22
runmad Avatar answered Nov 15 '22 09:11

runmad


Try these changes:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];

-(void)doneButtonPressed:(id)sender
{
}

the target is passed the object that initiated the action (i.e. the UIBarButtonItem itself)

Secondly, set a breakpoint in your doneButtonPressed function. If, for example, the tableView is getting set to nil, then you'll see nothing happen. i.e. perhaps there is an issue with instantiating this view controller.

like image 41
CSmith Avatar answered Nov 15 '22 09:11

CSmith