[英]Is it possible to performSegue from the UIView XIB?
在我的ViewController中,我设置了:
self.view.addSubview(xib)
其中xib
是xib
class myView: UIView {
当用户单击那些XIB中的按钮时,可以执行segue吗? 因为当我尝试时,它说
Value of type myView has no member performSegue
解决此问题的另一种方法是什么?
您可以使用协议/代理来实现。
在您的UIView类中有一个带有方法的协议,例如: userDidClickButton()
并在您的viewController类中侦听委托回调。 创建UIView类的实例时,将委托分配给self示例: uiviewClassInstance.delegate = self
然后在您的视图控制器类中实现上述委托方法,并在其中调用perfromSegue
。
编辑:在您的UIView类内
protocol SomeProtocol {
// protocol definition goes here
func didClickSubmit()
}
var delegate:SomeProtocol
@IBAction func buttonClick(sender: UIButton) {
delegate.didClickSubmit()
}
查看控制器类:
public class YourViewController: UIViewController,SomeProtocol
let signUpView = SignUpView.signUpView()
singUpView.delegate = self
func didClickSubmit() {
// Perform your segue here
}
performSegue
是UIViewController
一部分。 UIView
无法执行segue。 尝试从关联的UIViewController
调用此方法
我花了几个小时来尝试学习授权,尽管我还没有完全解决这个问题。 我知道代码的全部意义。 这是有关如何实现此目标的备忘单:
目标 :要执行功能或将数据从UIView传递回它所在的UIViewController。
示例 :您有一个带有UIView的UIViewController,您想按一下UIView中的一个按钮并使UIViewController执行segue。
免责声明:这个示例很is脚,因为您可以通过情节提要将按钮从UIView直接连接到UIViewController,但您会有所想法。
要完成该示例,请执行以下步骤:
1.在您的UIView类中,您将要编写以下代码(这是sender类):
import UIKit //written for reference on file scope
// Define protocol outside of class
protocol SomeUIViewDelegate {
func segueFunction() //this function will be in your receiving class
}
class SomeUIView: UIView {
//define the delegate as such:
var delegate: SomeUIViewDelegate?
//fake button press function I made for the tutorial
func someButtonPress(){
//this code goes wherever you want the delegate to "fire," like in this button press
delegate?.segueFunction()
}
}
2.在您的UIViewController类中,您将要编写以下代码(这是接收器类):
import UIKit //written for reference on file scope
//state the class will conform to the delegate
class SomeUIViewController: UIViewController, SomeUIViewDelegate {
//IMPORTANT: you must instantiate the SomeUIView class!
//use storyboard to right click drag and drop a line onto assistant editor, then name the instance
@IBOutlet weak var SomeUIViewNamedInstance
override func viewDidLoad() {
super.viewDidLoad()
//set the classes delegate to itself in viewDidLoad
SomeUIViewNamedInstance.delegate = self
}
//this is the function we defined in the protocol in SomeUIView class, it executes whenever you call it from the sender class
func segueFunction() {
//perform segue or whatever else you want
//EXTRA CREDIT: You can pass data too from sender to receiver within the function parameters!!
}
}
编辑:想要将这个关于授权的出色文章添加到这个答案中,以产生好奇心。 http://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/
是的,你可以,你可以打电话来进行。
当用户单击按钮时,您可以调用segue,如下所示:
self.storyboard(performSegueWithIdentifier("identifier", sender: self))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.