gpt4 book ai didi

当前的 iOS 10 Swift 3 UIViewController 不起作用

转载 作者:行者123 更新时间:2023-11-28 09:26:09 25 4
gpt4 key购买 nike

我目前正在使用 iOS 10 和 Swift 3 开发一个应用程序

我认为我可能破坏了 Controller 之间的导航。

确实,当我尝试呈现一个新的 View Controller 时,我在 Xcode 调试器上收到了这个警告。

Warning: Attempt to present FirstViewController on Project.StoryBoardManager whose view is not in the window hierarchy!

我做了一些研究,但我无法修复我的错误。

我的 AppDelegate.swift 上有这个

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let storyboard = UIStoryboard(name:"Authentication", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as UIViewController!

self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

return true
}

然后在我的类里面展示新闻观点

class StoryBoardManager: UIViewController{

fileprivate var appD = UIApplication.shared.delegate as! AppDelegate

func changeView(storyboardName: String){

let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let vc = storyboard.instantiateInitialViewController() {

vc.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
vc.modalPresentationStyle = UIModalPresentationStyle.fullScreen

//appD.window?.rootViewController = vc
present(vc, animated: true, completion: nil)

} else {
print("Unable to instantiate VC from \(storyboardName) storyboard")
}
}
override func viewDidLoad(){
super.viewDidLoad()
}

如果我评论 rootViewController 的更新,则不会显示新 Controller 。

为@Zac Kwan 编辑

import Foundation
import UIKit

class CustomNavBar: UIView {

fileprivate let _storyBoardManager : StoryBoardManager = StoryBoardManager()
fileprivate var _currentUIViewController : UIViewController = UIViewController()

init() {
super.init(frame: CGRect(x: 0, y: 0, width:0, height:0))
}

func changeViewStoryboard(sender: UIButton!){

if (sender.tag == 0){
self._storyBoardManager.changeView(storyboardName: "View1")
} else if (sender.tag == 1) {
self._storyBoardManager.changeView(storyboardName: "View2")
} else if (sender.tag == 2) {
self._storyBoardManager.changeView(storyboardName: "View3")
} else {
self._storyBoardManager.changeView(storyboardName: "View4")
}
}

override init(frame: CGRect) {
super.init(frame: frame)
}

func createButton(title: String, posX: Double, witdh: Double, tag: Int, font: UIFont) -> UIButton {

let buttonCreated = UIButton(frame: CGRect(x: posX, y: 0, width: witdh, height: 60))
buttonCreated.setTitle(title, for: UIControlState())
buttonCreated.setTitleColor(CommonVariable.darkGrey, for: UIControlState())
buttonCreated.titleLabel!.font = font
buttonCreated.tag = tag
buttonCreated.addTarget(self, action:#selector(self.changeViewStoryboard(sender:)), for: UIControlEvents.touchUpInside)

buttonCreated.backgroundColor = UIColor.white

return buttonCreated

}

required init?(coder aDecoder: NSCoder) {
Super. Inuit (coder: decoder)

addSubview(self.createButton(title: « ChangeView », posX: 256.0, witdh: Double(self._sizeButton) - 1, tag: 1, font: UIFont(name: « Arial », size: 15)!))
addSubview(self.createButton(title: « ChangeView 2 », posX: 512.0, witdh: Double(self._sizeButton) - 1, tag: 2, font: UIFont(name: « Arial », size: 15)!))
}

}

最佳答案

请尝试更改如下代码:

class StoryBoardManager: UIViewController{

fileprivate var appD = UIApplication.shared.delegate as! AppDelegate

func changeView(storyboardName: String){

let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let vc = storyboard.instantiateInitialViewController() {

vc.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
vc.modalPresentationStyle = UIModalPresentationStyle.fullScreen

//appD.window?.rootViewController = vc
appD.present(vc, animated: true, completion: nil)

} else {
print("Unable to instantiate VC from \(storyboardName) storyboard")
}
}
override func viewDidLoad(){
super.viewDidLoad()
}

关于当前的 iOS 10 Swift 3 UIViewController 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40219812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com