问题
I have viewController with segue to secondViewController named "toSecond". In viewController i load customView.xib
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]
in this customView i have button with action:
viewController().goToSecond()
in viewController i have function with this code
func goToSecond() {
self.performSegueWithIdentifier("toSecond", sender: self)
}
but when i pressed button in customView i become a error:
viewController has no segue with identifier 'toSecond'
when i call this function directly from viewController all work great!
So, how can i call performSegueWithIdentifier from my customView?
customView source code:
import UIKit
class customView: UIView {
@IBAction func ToSecondButton(sender: AnyObject) {
viewController().goToSecond() }
}
viewController source code:
import UIKit
class viewController: UIViewController {
...
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0]
self.view.addSubview(myCustomView)
func goToSecond() {
self.performSegueWithIdentifier("toSecond", sender: self)
}
...
}
回答1:
The problem is that your UIView
subclass is calling viewController().goToSecond()
. That's not doing what you think it is. The viewController()
isn't referencing the view controller that loaded your custom view. It's instantiating a second, orphaned instance of that class (not connected to any storyboard) and therefore cannot find the segue.
If you're really going to have this custom UIView
subclass initiate a segue, you need to pass a reference to your original view controller to the custom view. So add a property to the custom view subclass that can hold the reference to its view controller, and when the view controller instantiates this custom view, it has to set that property.
For example:
import UIKit
protocol CustomViewDelegate: class { // make this class protocol so you can create `weak` reference
func goToNextScene()
}
class CustomView: UIView {
weak var delegate: CustomViewDelegate? // make this `weak` to avoid strong reference cycle b/w view controller and its views
@IBAction func toSecondButton(sender: AnyObject) {
delegate?.goToNextScene()
}
}
And then
import UIKit
class ViewController: UIViewController, CustomViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let myCustomView = NSBundle.mainBundle().loadNibNamed("customView", owner: self, options: nil)[0] as! CustomView
myCustomView.delegate = self
// ... do whatever else you want with this custom view, adding it to your view hierarchy
}
func goToNextScene() {
performSegueWithIdentifier("toSecond", sender: self)
}
...
}
来源:https://stackoverflow.com/questions/36095510/how-to-call-performseguewithidentifier-from-xib