gpt4 book ai didi

ios - 在 Swift 3.0 中创建 AppDelegate 后后退按钮消失

转载 作者:行者123 更新时间:2023-12-01 17:47:58 26 4
gpt4 key购买 nike

我在 xcode 8 swift 3.0 中创建了如下结构。

在我添加 之前应用委托(delegate) 代码。 “应用”、“代表应用”和“配置文件” Controller 上的后退按钮仍然显示正常。

我使用segue打开页面。

但是在我添加 之后应用委托(delegate) 代码进入主页和登录 Controller ,返回按钮在 上消失申请,代申请简介 Controller 页面。

有人可以帮助或解释为什么会这样吗?如何在申请,代表申请和个人资料页面上启用后退按钮?

谢谢。

enter image description here

首页.swift

import UIKit
class ViewController: UIViewController {

@IBOutlet var staffNumberLbl: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
staffNumberLbl.text = UserDefaults.standard.object(forKey: "login") as? String

}
override func viewDidAppear(_ animated: Bool) {
let isUserLoggedIn = UserDefaults.standard.bool(forKey: "loggedIn")
if(!isUserLoggedIn){
self.performSegue(withIdentifier: "loginview", sender: self)
}
}

@IBAction func logoutData(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "loggedIn")
UserDefaults.standard.synchronize();
let loginViewController = self.storyboard!.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
}
}

登录.swift
import UIKit

class LoginViewController: UIViewController {
@IBOutlet var loginlbl: UITextField!
@IBOutlet var passlbl: UITextField!
@IBOutlet var login_button: UIButton!

var login: String!
var pw: String!

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func loginData(_ sender: Any) {

login = loginLbl.text
pw = passLbl.text

if(login == "" || pw == ""){
return
}
else{
let url = URL(string: "http://localhost/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"

let LoginDataToPost = "login=\(login!)&pw=\(pw!)"
request.httpBody = LoginDataToPost.data(using: String.Encoding.utf8)

let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil {
return
}
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
{
DispatchQueue.main.async
{
let message = Int(json["message"]!)
let login = json["login"]

if(message == 1) {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.set(login, forKey: "login")
UserDefaults.standard.synchronize();
self.dismiss(animated: true, completion: nil)
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = myViewController
appDelegate.window?.makeKeyAndVisible()

print("Value for login is : \(login!)")

return
}
else {}
}
}
else {}
}
catch let jsonParse {}
}
})
task.resume()
}
}
}

AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


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

let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let isUserLoggedIn:Bool = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
if(!isUserLoggedIn) {

let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}
else {
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window!.rootViewController = homePage
window!.makeKeyAndVisible()
}
return true
}
}

最佳答案

您正在设置 rootviewcontroller 而不在 logoutData 中嵌入导航 Controller & loginData功能。

使用此代码:

let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController

在 AppDelegate 中使用此代码:
if(!isUserLoggedIn) {

let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let navigationController = UINavigationController.init(rootViewController: loginViewController)
appDelegate.window?.rootViewController = navigationController
window!.makeKeyAndVisible()
}
else {
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navigationController = UINavigationController.init(rootViewController: homePage)
appDelegate.window?.rootViewController = navigationController
window!.makeKeyAndVisible()
}

关于ios - 在 Swift 3.0 中创建 AppDelegate 后后退按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42082935/

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