UINavigationController appDelegate UIViewController 以编程方式
UINavigationController appDelegate UIViewController programmatically
我正在尝试以编程方式创建一个 UINavigationController 和两个 UIVewController,并能够使用导航控制器从第一个 VC 转换到第二个 VC 并返回。我已经尝试了不同的方法来让它工作。我在源代码中留下了一些注释行,以提示我尝试过的事情。
使用当前方法我收到此警告(不起作用):
Warning: Attempt to present <x.VC2: 0x7fd27240df00> on <x.VC1: 0x7fd27240db60> whose view is not in the window hierarchy!
在AppDelegate.swift中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navcon = UINavigationController()
//navcon.viewControllers = [VC1(), VC2()] // tried this without success
navcon.viewControllers = [VC1()]
window = UIWindow()
window?.makeKeyAndVisible()
//window?.rootViewController = VC1() // one variation that I tried
window?.rootViewController = navcon.viewControllers[0]
return true
}
在VC1.swift中:
var navcon: UINavigationController!
let vc2 = VC2()
//---------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
navcon = self.navigationController
//navcon?.viewControllers.append(vc2)
let x = navcon?.viewControllers
let n = x?.count
}
//---------------------------------------------
func triggeredFunction() {
self.present(vc2, animated: true, completion: nil)
//navcon.pushViewController(vc2, animated: true)
}
结果:
代码:
AppDelegate:
window = UIWindow()
let firstVC = FirstViewController()
let navigationController = UINavigationController(rootViewController: firstVC)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
第一VC:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
button.backgroundColor = .red
view.addSubview(button)
button.addTarget(self, action: #selector(showSecondVC), for: .touchUpInside)
}
@objc func showSecondVC() {
let secondVC = SecondViewController()
navigationController?.pushViewController(secondVC, animated: true)
}
第二个VC:
view.backgroundColor = .yellow
希望对您有所帮助!请让我知道它是否适合您。
我正在尝试以编程方式创建一个 UINavigationController 和两个 UIVewController,并能够使用导航控制器从第一个 VC 转换到第二个 VC 并返回。我已经尝试了不同的方法来让它工作。我在源代码中留下了一些注释行,以提示我尝试过的事情。
使用当前方法我收到此警告(不起作用):
Warning: Attempt to present <x.VC2: 0x7fd27240df00> on <x.VC1: 0x7fd27240db60> whose view is not in the window hierarchy!
在AppDelegate.swift中:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navcon = UINavigationController()
//navcon.viewControllers = [VC1(), VC2()] // tried this without success
navcon.viewControllers = [VC1()]
window = UIWindow()
window?.makeKeyAndVisible()
//window?.rootViewController = VC1() // one variation that I tried
window?.rootViewController = navcon.viewControllers[0]
return true
}
在VC1.swift中:
var navcon: UINavigationController!
let vc2 = VC2()
//---------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
navcon = self.navigationController
//navcon?.viewControllers.append(vc2)
let x = navcon?.viewControllers
let n = x?.count
}
//---------------------------------------------
func triggeredFunction() {
self.present(vc2, animated: true, completion: nil)
//navcon.pushViewController(vc2, animated: true)
}
结果:
代码:
AppDelegate:
window = UIWindow()
let firstVC = FirstViewController()
let navigationController = UINavigationController(rootViewController: firstVC)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
第一VC:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
button.backgroundColor = .red
view.addSubview(button)
button.addTarget(self, action: #selector(showSecondVC), for: .touchUpInside)
}
@objc func showSecondVC() {
let secondVC = SecondViewController()
navigationController?.pushViewController(secondVC, animated: true)
}
第二个VC:
view.backgroundColor = .yellow
希望对您有所帮助!请让我知道它是否适合您。