New view from UIBarButtonItem?

2019-08-31 02:24发布

问题:

So I've seen previous questions similar to this but they were of no help. I've read Apple's documentation too but I could not understand where I've gone wrong. AFAIK I did everything logically, but when I click on my done button on an UItoolbar overlay, the button can be pushed but it does not do anything. This obviously means it fails to acknowledge the written code. But how?

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.

-(void)doneButtonPressed {

TableViewController *UIView = [[TableViewController alloc]
initWithNibName:@"TableViewController" bundle:nil];
UIView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:UIView animated:YES];
[UIView release];

}

回答1:

Whoa, you've got some bizarre stuff going on here. In your first line, you're allocating and initiating the TableViewController instance correctly, but you're not giving that instance a unique name. You're naming it with another class's name, which is bound to stir up problems. In fact, I'm surprised it didn't through an error.

Try the following instead:

TableViewController *tableView = [[TableViewController alloc]
initWithNibName:@"TableViewController" bundle:nil];
tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tableView animated:YES];

Now, your TableViewController instance has a unique name that is referenced throughout the rest of the method. Just to be clear--UIView is another class name, and therefore cannot be used as the name of an instance of an object.

EDIT: Additionally, be sure to add your button's selector doneButtonPressed: to your .h file of its view controller. Also, if you like you can toss an NSLog() call in the beginning of the function just to be sure it isn't (or perhaps is) being called.



回答2:

Something to check when button actions aren't firing is that you've got the appropriate selector. If you've followed the selector correctly. Make sure you aren't using a selector of

@selector(doneButtonPressed:)

which would look for a function like:-

- (void) doneButtonPressed:(id) sender

For your member function, you need

@selector (doneButtonPressed)

The debugger is your friend here. Start with a breakpoint to make sure your function is being called.

If you're getting into the function, then The Kraken's answer is the next thing to check.



回答3:

There is no restriction on using a class name as a variable name whatsoever. Although you should change it because its confusing and doesnt follow iOS coding conventions.

"Button can be pushed but doesnt do anything", is the selector even being called?

-(void)doneButtonPressed

Show how you created the UIBarButtonItem to verify that you provided the right selector in the init method or that you connected the button directly in interface builder (which it doesnt look like since you didnt use the (IBAction) return signature.