问题
My question is very similar to : Swift: Make button trigger segue to new scene
The issue is:
I have a view controller, with a button that causes another view controller to appear modally.
I have Ctrl+Click from the button to the second View Controller, and created the segue in IB.
Then I Ctrl+Click from the button again to the source code of the view controller to create an IBAction method.
I assumed that the button will do two things now: a) call the IBAction method , and b) perform the segue.
What happens is only the segue for some reason.
When I delete the segue, or remove the call to the view controller from IB, then the IBAction is called, but Xcode tells me that the second view controller is not reachable now.
I want to be able to present an ActionSheet to the user and then be able to performSegue to the second view controller, based on what the user selected from the action sheet.
I know I can programatically call performSegue
but that requires the creation of the segue and attaching it to a physical button in IB, which defeats the purpose of not calling the IBAction that button may already have.
回答1:
If I want to do some additional steps before calling the segue I usually attach an IBAction to the UIButton and call the perform segue from within this, in the code. You can add a segue to the storyboard and give it an ID, without having to connect it to a button. You do this by control-clicking on the viewcontroller and drag to the next viewcontroller in the storyboard.
回答2:
Connecting a segue from storyboard will automatically always perform the segue. You can do any preparation for transitioning to the new view controller in the prepareForSegue
method.
If there is some logic that comes before, like a user selecting something from the action sheet, just use the IBAction and then perform the segue with performSegueWithIdentifier
based on the user selection. However, you do not have to create another button, just ctrl+drag
from one view controller to another and give the segue an identifier.
来源:https://stackoverflow.com/questions/31304813/why-uibarbuttonitem-doesnt-call-ibaction-before-segue