gpt4 book ai didi

Swift:无法以编程方式推送查看 Controller

转载 作者:行者123 更新时间:2023-11-28 14:12:46 24 4
gpt4 key购买 nike

我正在尝试使用此代码以编程方式推送到不同的 View Controller :

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)

这是我的设置:我有一个名为 MenuController 的 swift 文件,其中包含此代码 我将标识符名称设置为:LoginViewController 我设置了两个 的类ViewControllers 设置为 MenuController。但每当我按下按钮触发事件时,它什么都不做。

这是我要求的其余代码:

import UIKit

class MenuController: UIViewController {

var thingy = 0

@IBAction func ThingyBttn(_ sender: UIButton) {
if(thingy == 1) {
print("ok")
}else{
print("good")

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
}
}

@IBOutlet var myButton: UIButton!

@IBAction func Button(_ sender: UIButton) {
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
thingy = 1

print("1")
} else {
thingy = 2

print("2")
}
}

}

我知道代码的其他所有内容都可以正常工作,因为它正在打印消息。

最佳答案

您需要像这样从 navigationController 中插入您执行推送的 vc

self.navigationController?

nil ,或者以编程方式执行

编辑:

替换这个

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
let nav = UINavigationController(rootViewController:self)
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav
nav.pushViewController(loginVC, animated: true)

关于Swift:无法以编程方式推送查看 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52389144/

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