【问题标题】:Framework ViewController View Not showing框架 ViewController 视图未显示
【发布时间】:2019-08-21 22:46:42
【问题描述】:

我正在尝试创建一个包含视图控制器的框架(Login VC)。我已成功导入框架并呈现 VC,但视图未显示。我在导入的 viewDidLoad 中有一个打印功能,它正在打印。我错过了什么?

框架 VC:

public class LoginVC: UIViewController {

    @IBOutlet weak var button: UIButton! {
        didSet {
            button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        }
    }

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func viewDidLoad() {
        super.viewDidLoad()
        print("View Loaded") // Firing
    }

    @objc func buttonPressed() {
        print("hello")
    }

}

框架 VC Xib:

这是我展示框架 VC 时的视图调试器

-- 更新:这就是我展示 VC 的方式 ---

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let homeViewController = LoginVC()
    homeViewController.view.backgroundColor = UIColor.white
    window!.rootViewController = homeViewController
    window!.makeKeyAndVisible()
    return true
}

-- 更新--

由于许多 cmets 与应用程序委托相关,我首先展示一个通用 ViewController,然后它会展示我的登录框架 VC。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let homeViewController = ViewController()
    homeViewController.view.backgroundColor = UIColor.white
    window!.rootViewController = homeViewController
    window!.makeKeyAndVisible()
    return true
}


class ViewController: UIViewController {

   @IBOutlet weak var button: UIButton! {
       didSet {
           button.addTarget(self, action: #selector(presentNext), for: .touchUpInside)
       }
   }

    @objc func presentNext() {
        let loginVC = LoginVC()
        present(loginVC, animated: true, completion: nil)
    }

}

现在,当我展示登录框架时,我得到的只是黑屏。

-- 更新--

我可以在 viewdidLoad 中更改视图的背景颜色,但不会显示 xib 视图。这是为什么呢?..

【问题讨论】:

  • 你说你“成功地介绍了 VC”……你是怎么做到的?
  • @LorenzoSantini 更新了问题——我在应用程序委托中显示它。
  • 我假设您忘记在“window”前面插入“let”...或者您是否在其他地方声明了它?
  • @LorenzoSantini 这是一个类属性。代码编译并构建。
  • 你用的是什么版本的swift?

标签: ios swift uiviewcontroller frameworks


【解决方案1】:

带有连接到 viewController 的 xib 的框架需要从包中显式加载。

通常当我们使用 xib 创建 cocoaTouch UIViewController 时,这是为我们处理的。但是,在使用框架的时候,就不处理了。

为了解决这个问题,我在 viewDidLoad 中添加了加载 xib

override func viewDidLoad() {
    super.viewDidLoad()

    // Frameworks require loading of the xib to connect xib to view controller
    let bundle = Bundle(for: type(of: self))
    bundle.loadNibNamed("viewControllerName", owner: self, options: nil)
}

【讨论】:

    【解决方案2】:

    您需要在 ViewController 实际出现后调用您的 presentNext() -- 不在 viewDidLoad 中,甚至在 >viewWillAppear

    像这样:

    class ViewController: UIViewController {
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            presentNext()
        }
        @objc func presentNext() {
            let loginVC = LoginVC()
            present(loginVC, animated: true, completion: nil)
        }
    
    }
    

    这是工作测试项目:

    https://github.com/drewster99/SO_LoginVC

    另外,可能会仔细检查您的 ViewController.xib 是否为按钮实际附加了 IBOutlet。它实际上必须是这样的。其他一切看起来都不错。

    这是我得到的:

    AppDelegate:

    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    
        var window: UIWindow?
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
            window = UIWindow(frame: UIScreen.main.bounds)
            let homeViewController = ViewController()
            homeViewController.view.backgroundColor = UIColor.white
            window!.rootViewController = homeViewController
            window!.makeKeyAndVisible()
            return true
        }
    }
    

    ViewController.swift:

    import UIKit
    import LoginFrameworkThing
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var button: UIButton! {
            didSet {
                button.addTarget(self, action: #selector(presentNext), for: .touchUpInside)
            }
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
        }
    
        @objc func presentNext() {
            print("Presenting next...")
            let loginVC = LoginVC()
            present(loginVC, animated: true, completion: nil)
        }
    }
    

    按钮连接在 ViewController 中,框架中存在 LoginVC(.xib 和 .swift)。

    我更新了示例项目。检查链接。

    【讨论】:

    • 我把它附加到一个按钮上(因此是@objc)..对不起,我把它漏掉了,因为我认为它会是样板文件..
    • 我认为这绝对是 IBOutlet。查看更新的链接或查看上面的更新答案。
    • 我检查了它——它没有构建(没有这样的模块 LoginFrameworkThing 作为导入)。此外,没有可以运行该方案的模拟器列表。很奇怪。
    • 你能显示 xib 视图,还是只改变视图背景颜色?
    • @Mocha 重新克隆存储库。有一个错字和更新的项目设置(没有推送最新的)。 git@github.com:drewster99/SO_LoginVC.git
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多