如何从 xib: UIView 文件加载视图控制器? swift 3.



好的,我这里有 2 个问题:

1 我尝试从 xib 文件中的按钮加载视图控制器。 这是我的 xib 中的按钮:UIView。

@IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
self.presentViewController(vc, animated: true, completion: nil)
}

这里的问题是presentViewController因为我的 xib 是 UIView 类型,他向我显示此错误:"(值类型视图控制器)没有当前视图控制器的成员"。 为什么我尝试使用此代码?是因为在我的日历视图控制器中,按下按钮时我有以下代码:dismiss(animated: true, completion: nil)所以这是我的第一个问题......

阿拉伯数字 第二个问题是当我在 xib 内部的按钮中有这段代码时:UIView,日历视图控制器加载正常......但是代码:dismiss(animated: true, completion: nil)不再工作了。 这是我的按钮xib的代码:

@IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
self.window!.rootViewController = vc
}

所以。。如何加载视图控制器(日历视图控制器)和关闭代码继续运行?

好的...我有我需要的东西。 对于这项工作,我在视图控制器中使用UITapGestureRecognizer,它保存视图:

override func viewDidLoad()输入以下代码:

let fechaArriboTap = UITapGestureRecognizer(target: self, action:#selector(handleArribo))
let fechaSalidaTap = UITapGestureRecognizer(target: self, action:#selector(handleSalida))
fechaArribo.addGestureRecognizer(fechaArriboTap)
fechaSalida.addGestureRecognizer(fechaSalidaTap)

然后创建方法:

func handleArribo() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
func handleSalida() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}

就是这样!谢谢@sasquatch

最新更新