【问题标题】:Reuse parent ViewController storyboard file in Child ViewController在子 ViewController 中重用父 ViewController 故事板文件
【发布时间】:2018-10-07 12:55:04
【问题描述】:

使用 storyBoard 展示 ViewController: 如果 newViewController 在 StoryBoard 中。我们可以使用以下方法呈现它。

 let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
let newViewcontroller = storyboard.instantiateInitialViewController() as? AViewController
  self.present(newViewcontroller, animated: false, completion: nil)

是否可以呈现不在storyBoard中但其父viewcontroller有storyboard的ViewController

我以编程方式创建了一个 B 视图控制器(没有 StoryBoard),现在我想展示 BViewController,它应该使用 AViewController StoryBoard?

class AViewController : UIViewController
{
//this class is in storyboard which is generic design that I want to use it in different ways.
}
class BViewController : AViewController
{
....//
}
e.g.
 self.present(BViewController(), animated: false, completion: nil)?

当我展示 BViewcontroller 时,它会为来自超类的参数抛出 nil。

【问题讨论】:

  • 按照您在问题中显示的方式进行操作有什么问题?
  • 当你有类似的 viewcontroller 有小的改动时。你必须创建不同的视图控制器

标签: ios swift iphone uiviewcontroller reusability


【解决方案1】:

如果您的 ViewController 确实不在 Storyboard 中,您的代码将可以正常工作。您所要做的就是在没有nibName 的情况下实例化它,如下所示:

class ViewController: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
}

普通 Storyboard ViewControllers 依赖于 coder 初始化创建,但您可以使用 nibName: nil 初始化程序正常创建它们,只要您没有任何出口(这是您的情况,因为 ViewController不在故事板上)

【讨论】:

    【解决方案2】:

    一切皆有可能以编程方式实现。并非一切都可以通过 Interface Builder 实现。故事板只是 Interface Builder 的一个组件。

    从根以编程方式呈现视图控制器:

    @objc private func presentViewController() {
    
        guard let root = UIApplication.shared.keyWindow!.rootViewController else {
            return
        }
    
        let destination = SomeViewController()
        destination.transitioningDelegate = YourAnimationVendor()
        destination.modalPresentationStyle = .custom
        root.present(destination, animated: true, completion: nil)
    
    }
    

    您可以省略过渡委托(和自定义动画供应商)并使用库存 UIKit 动画呈现,例如 .fullScreen 而不是 .custom

    @objc private func presentViewController() {
    
        guard let root = UIApplication.shared.keyWindow!.rootViewController else {
            return
        }
    
        let destination = SomeViewController()
        destination.modalPresentationStyle = .fullScreen
        root.present(destination, animated: true, completion: nil)
    
    }
    

    【讨论】:

      【解决方案3】:

      我创建了复用故事板文件或继承故事板文件的答案。

      object_setClass(设置对象的类。)将使用 BViewController 类覆盖 AViewController 的实例。因此,您可以在 AViewController 之上添加更多方法。

      当您有类似的视图控制器并进行了小的更改时。您必须创建不同的视图控制器。使用此方法,您可以在带有情节提要的基本视图控制器上创建并重用该视图控制器。

       class BViewController{
             static func vcInstanceFromStoryboard() ->BViewController? {
                  let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
                  let instance = storyboard.instantiateInitialViewController() as? AViewController
                  object_setClass(instance, BViewController.self) // 
                  return (instance as? BViewController)!
              }
          .....
          } 
      
      This is an example of how do we use it:
      let vc = BViewController.vcInstanceFromStoryboard()
      self.present(vc , animation : true)
      

      【讨论】:

        猜你喜欢
        • 2012-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 2012-01-17
        相关资源
        最近更新 更多