我正在尝试创建一个包含视图控制器的框架(登录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
}
-更新-
由于许多评论与应用程序委托有关,因此我首先介绍一个通用的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视图。为什么会这样?..
答案 0 :(得分:2)
将xib连接到viewController的框架需要从捆绑中显式加载。
通常,当我们使用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)
}
答案 1 :(得分:1)
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)。
我更新了示例项目。检查链接。