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.
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.
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:
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With