提问者:小点点

导航控制器未加载(push,present)另一个viewController


我无法在单击按钮时加载viewController,因为我知道定义的Vc包含我想要呈现的Vc的值。

//AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController") as! ViewController
    let navVC = UINavigationController.init(rootViewController: mainViewController)
    self.window?.rootViewController = navVC
    self.window?.makeKeyAndVisible()
    return true
}

 //ViewController Class
 class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var pageController: UIPageControl!

private let reuseIdentifier = /*"CollectionViewCell"*/ "Cell"
let collectionViewImages : [UIImage] = [#imageLiteral(resourceName: "cooov"), #imageLiteral(resourceName: "shutterstock535739452"), #imageLiteral(resourceName: "portraitYoungManLyingBedCheckingHisFeverThermometer232147948490")]
let collectionViewTexts : [String] = [
    "Stay at home ! your health is our Priority",
    "Access Latest Coronavirus articles !",
    "Check up on your health on a daily basis !"
]

override func viewDidLoad() {
    super.viewDidLoad()
    renderButton()
    renderCell()
    pageController.numberOfPages = collectionViewImages.count
    pageController.currentPage = 0
}

@IBAction func buttonClicked(_ sender: UIButton) {
    let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
    as! SignUp
self.navigationController?.pushViewController(signupVC, animated: true)
}
 }


//SignUp class The VC that should appear on ButtonClick 
class SignUp : BaseViewController {
override func viewDidLoad() {
    super.viewDidLoad()
 }
}

 //the class that is inherited by ViewController
 class BaseViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.isHidden = true
 }
 }
[enter image description here][1]}

在此处输入图像说明

这里有一个链接到提供的图片,请检查。 https://drive.google.com/drive/folders/1CXYSH911PIA3-S9DX6EUV7DE8VLNED4L?USP=共享


共2个答案

匿名用户

这是一个函数,你可以这样改变它,然后告诉我结果

首先运行这个

@IBAction func buttonClicked(_ sender: UIButton) {
        let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")

        let navVC = UINavigationController.init(rootViewController: signupVC)
      //  self.present(navVC, animated: true, completion: nil)

        if let nav =  self.navigationController {
            nav.pushViewController(navVC, animated: true)
        } else {
            print("navigation controller not found")
        }
    }

然后试着告诉我。。。让我知道发生了什么

@IBAction func buttonClicked(_ sender: UIButton) {
        let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")

        let navVC = UINavigationController.init(rootViewController: signupVC)
        self.present(navVC, animated: true, completion: nil)


    }

匿名用户

你不需要新的导航控制器来推动。

@IBAction func buttonClicked(_ sender: UIButton) {
    let signupVC = UIStoryboard.init(name:"SignUp", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
        as! SignUp
    self.navigationController?.pushViewController(signupVC, animated: true)
}

另外,如果您使用的是iOS13.0+并使用Xcode11+,您的SceneDelegate将被调用,而不是AppDelegate。 因此,尝试将粘贴复制到SceneDelegate。