在我的ViewController中,我设置了:
foreach
其中self.view.addSubview(xib)
是xib
当用户点击那些XIB中的按钮时,我可以执行segue吗?因为当我尝试时,它会说
class myView: UIView {
解决这个问题的另一种方法是什么?
答案 0 :(得分:3)
您可以使用协议/代表来实现。
在你的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
}
答案 1 :(得分:1)
performSegue
是UIViewController
的一部分。 UIView
无法执行segue。尝试从关联的UIViewController
答案 2 :(得分:1)
我花了好几个小时试图学习代表团,虽然我还没有完全绕过它。我知道代码是什么。以下是关于如何实现这一目标的备忘单:
目标:执行一项功能或将数据从UIView传回给它所在的UIViewController。
示例:你有一个带有UIView的UIViewController,你想按下UIView中的一个按钮并使UIViewController执行一个segue。
免责声明:这个特殊的例子是蹩脚的,因为你可以通过故事板将UIView直接按钮连接到UIViewController,但你明白了。
要完成该示例,请按照以下步骤操作:
1。在您的UIView课程中,您需要编写此内容(这是发件人类):
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/
答案 3 :(得分:0)
是的,你可以,你可以称之为发生。
当用户点击按钮时,您可以调用segue,如下所示:
String s="1234990912349909";
s=s.replaceAll("9909","");
int a=Integer.parseInt(s);
System.out.println(a);
//output would be 12341234