【问题标题】:how to call xib file from storyboard on button click in swift如何在快速单击按钮时从情节提要中调用xib文件
【发布时间】:2020-03-27 20:22:21
【问题描述】:

我是 swift 的新手,我想在单击按钮时从情节提要中调用 .xib 文件。 我正在使用这样的代码,但它崩溃了,因为 .xib 文件不在情节提要中

 let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let newViewController = storyBoard.instantiateViewController(withIdentifier: "abcViewController") as! abcViewController
 self.present(newViewController, animated: true, completion: nil)

这段代码

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 self.present(newViewController, animated: true, completion: nil)

这样的画面

我只是换线,现在它工作正常

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 //self.present(newViewController, animated: true, completion: nil)

 self.navigationController?.pushViewController(newViewController, animated: true)

【问题讨论】:

  • 故事板里面有 xib 吗?
  • @Sailendra 不,它不在里面。
  • 如果我错了,请纠正我,你在故事板中有一个按钮,你想在按钮点击右键时加载一个 xib?
  • @Sailendra 完成了,谢谢您的宝贵时间。

标签: ios swift storyboard xib


【解决方案1】:

@Mujtaba 解决屏幕不显示全屏的问题转到故事板中的视图控制器并将演示样式更改为全屏

【讨论】:

    【解决方案2】:
    let newViewController = abcViewController(nibName: "xibFileName", bundle: nil)
    //If you want to present xib
    self.present(newViewController, animated: true, completion: nil)
    
    //If you want to push xib in navigation stack
    self.navigationController?.pushViewController(newViewController, animated: true)
    

    【讨论】:

    • 我编辑了它没有正确显示屏幕的问题。
    • 添加cmets,解释你的答案,阅读how to answer
    【解决方案3】:

    可以试试这个:

    // Register Xib
    let abcViewController = ABCViewController(nibName: "abcViewController", bundle: nil)
    // Present VC "Modally" style
    self.present(abcViewController, animated: true, completion: nil)
    

    将ID设置为abcViewController与类名相同

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2013-11-10
      • 1970-01-01
      • 2021-08-01
      • 2021-09-26
      • 1970-01-01
      相关资源
      最近更新 更多