如何从UIView NOT UIViewController
内部正确准备segue我的UIViewController有一个容器视图,在容器视图里面有一个按钮。
class myInnerView: UIView {
...
func myButton(gesture: UIGestureRecognizer){
//calling a perform segue from another UIViewController does not recognize the SegueID
//ViewController().self.showProfile(self.id) -- DOES NOT WORK
}
}
class ViewController: UIViewController {
...
func showProfile(id: String){
...
self.performSegueWithIdentifier("toViewProfile", sender: self)
}
}
答案 0 :(得分:4)
您的视图不应处理按钮。这应该由控制器处理。 (这就是它被称为“控制器”的原因,而UIView
被称为“视图”)。
答案 1 :(得分:-1)
如果按钮是视图控制器视图的子视图,您应该能够将IBAction onTouchUpInside从按钮拖动到视图控制器。然后,您可以从该方法启动segue。
答案 2 :(得分:-1)
其中一个解决方案是将UITapGestureRecognizer
添加到您的按钮,但是从UIViewController
内部添加:
class ViewController: UIViewController {
var myInnerView = myInnerView()
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTapGesture))
self.myInnerView.myButton.addGestureRecognizer(tap)
}
@objc func handleTapGesture(){
performSegue(withIdentifier: "toViewProfile", sender: nil)
}
}
class myInnerView: UIView {
// make outlet of your button so you can add the tapGestureRecognizer from your ViewController and thats all
@IBOutlet public weak var myButton: UIButton!
}
答案 3 :(得分:-1)
您需要点按手势才能浏览视图
let customView = myInnerView()
let gestureRec = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
myView.addGestureRecognizer(customView)
func someAction(_ sender:UITapGestureRecognizer){
let controller = storyboard?.instantiateViewController(withIdentifier: "someViewController")
self.present(controller!, animated: true, completion: nil)
// swift 2
// self.presentViewController(controller, animated: true, completion: nil)
}