【问题标题】:how to load a ViewController from xib: UIView file? swift 3如何从 xib: UIView 文件加载 ViewController?迅速 3
【发布时间】:2017-12-09 17:04:39
【问题描述】:

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

1 我尝试从 xib 文件中的按钮加载 ViewController。 这是我的 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 类型,他告诉我这个错误:“(value type ViewController) has no member of presentViewController”。 为什么我尝试使用此代码?是因为在我的 CalendarioViewController 中按下按钮时我有这个代码:dismiss(animated: true, completion: nil) 所以这是我的第一个问题......

2 第二个问题是当我在 xib 内的按钮中有此代码时:UIView,CalendarioViewController 加载正常...但代码: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

}

那么..如何加载 ViewController (CalendarioViewController) 和关闭代码保持运行?

【问题讨论】:

  • 找到UIView的父VC,对VC执行动作
  • 对不起...我无法关注你...
  • 视图无法呈现视图控件。所以你必须找到持有该视图的视图控制器,然后从该父视图控制器中呈现另一个视图控制器
  • 啊...好吧,现在我明白了...我会尝试的!谢谢!
  • 我认为诀窍是如何从 rootViewController 中解散...此代码不起作用:self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)跨度>

标签: swift xib viewcontroller


【解决方案1】:

好的...我有我需要的东西。 对于这项工作,我在 View Controller 中使用 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

【讨论】:

    猜你喜欢
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多