ios - 从 nib 文件所有者加载 ViewController

标签 ios swift iphone xcode10 swift4.2

我有一个用 nib 制作的设计,围绕所有 ViewController,我正在加载该 nib 设计。

因为这是常见的页脚,所以我设法在所有其他 View Controller 的页脚中使用它。

现在我想要什么:我希望每当用户点击页脚时,它必须启动一个新的 View Controller ,它将显示您可以说的“关于我们” View Controller 。

我在做什么:

// ON CLICK OF FOOTER I AM DOING 
 let mAboutUs = self.storyboard?.instantiateViewController(withIdentifier: "idAboutUs") as! AboutUs
    mAboutUs.modalPresentationStyle = .fullScreen
    self.present(mAboutUs, animated: true) {

    }

但是我得到以下错误

Value of type 'FooterView' has no member 'storyboard'

我的理解:我认为从 nib 文件我们不能启动一个新的 ViewController,但我真的不想在我添加了这个 nib (FooterView) 的所有其他 View Controller 中做这件事作为我在每个 View Controller 底部的页脚 View 。

请帮帮我!!!

最佳答案

UIView 子类不包含 storyboard 属性,它用于 vc 子类,您需要

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "idAboutUs") as! AboutUs

如果你想在 View 中显示一个 vc FooterView 然后添加一个委托(delegate)

weak var delegate:VCName?

创建实例时

let ins = FooterView() // or from storyboard 
ins.delegate = self

然后使用

delegate?.present(mAboutUs, animated: true)

在 View 的类中


您可以使用所有 vcs 都符合的委托(delegate),但最简单的是添加此扩展

extension UIViewController {
    func topMostViewController() -> UIViewController {

        if let presented = self.presentedViewController {
            return presented.topMostViewController()
        }

        if let navigation = self as? UINavigationController {
            return navigation.visibleViewController?.topMostViewController() ?? navigation
        }

        if let tab = self as? UITabBarController {
            return tab.selectedViewController?.topMostViewController() ?? tab
        }

        return self
    }
}

然后

 guard let currentVC =  (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController.topMostViewController() else { return } 
 currentVC.present(mAboutUs, animated: true)

关于ios - 从 nib 文件所有者加载 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57675269/

相关文章:

iphone - 我应该使用哪个 iPhone API 来传输音频?

ios - 如何更改 UITextView 和 UITextField 中的文本

ios - 禁用在 iTunes 文件共享中添加文件

objective-c - UIGestureRecognizers 冲突

ios - 在 swift 中禁用 mmdrawer viewcontroller 菜单

ios - 使用 UIBezierPath 的 PrgressView

ios - 删除两个实体MagicalRecord之间的关系

ios - 如何更改键盘的宽度

iphone - "the timespace of the animation' s层”和 "in the local active time"是什么意思?

javascript - iOS 文件上传时缺少选项 "Upload Photo or Video"