在 Swift 3.0 中创建 AppDelegate 后后退按钮消失
Back button disappears after creating AppDelegate in Swift 3.0
我在 xcode 8 swift 3.0.
中创建了如下结构
在我添加 AppDelegate 代码之前。后退按钮在 Apply、Apply Behalf 和 Profile 控制器上仍然显示正常。
我用segue打开页面。
但是在我将 AppDelegate 代码添加到主页和登录控制器后,后退按钮在 应用、代表应用 和 profile 控制器页面。
有人可以帮助或解释为什么会这样吗?如何在申请、代表申请和个人资料页面上启用后退按钮?
谢谢。
Home.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()
}
}
Login.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
}
}
取决于您的配置:
self.navigationItem.hidesBackButton = YES;
或:
self.navigationController.navigationItem.hidesBackButton = YES;
或者,如果你只想禁用按钮而不隐藏它,你可以使用它。
self.navigationController.navigationItem.backBarButtonItem.enabled = NO;
您正在设置 rootviewcontroller,但没有在 logoutData
& 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()
}
成功登录后,尝试将 NavigationController 设置为 rootViewController 而不是您的 ViewController
您的后退按钮将开始出现。
在 AppDelegate 中,在 else 块中,而不是这一行
让主页=mainStoryBoard.instantiateViewController(withIdentifier:"ViewController")为! ViewController
写这个
让主页 = mainStoryBoard.instantiateViewController(withIdentifier: "NavigationController") 为! UINavigationController
在登录内部ViewController,在 if(message == 1)
块中
替换
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
和
let navController:UINavigationController = self.storyboard!.instantiateViewController(withIdentifier: "NavigationController") 作为! UINavigationController
还将故事板中 UINavigationController 的故事板标识符设置为 NavigationController
从 Home.swift 中删除此内容,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
因为它没有继承导航控制器的属性
并将其添加到 Appdelegate.swift 文件
对于其他3个视图控制器,您需要在每个Segway之间添加导航控制器以便继承它或通过分别实例化视图控制器来编码按钮
我在 xcode 8 swift 3.0.
中创建了如下结构在我添加 AppDelegate 代码之前。后退按钮在 Apply、Apply Behalf 和 Profile 控制器上仍然显示正常。
我用segue打开页面。
但是在我将 AppDelegate 代码添加到主页和登录控制器后,后退按钮在 应用、代表应用 和 profile 控制器页面。
有人可以帮助或解释为什么会这样吗?如何在申请、代表申请和个人资料页面上启用后退按钮?
谢谢。
Home.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()
}
}
Login.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
}
}
取决于您的配置:
self.navigationItem.hidesBackButton = YES;
或:
self.navigationController.navigationItem.hidesBackButton = YES;
或者,如果你只想禁用按钮而不隐藏它,你可以使用它。
self.navigationController.navigationItem.backBarButtonItem.enabled = NO;
您正在设置 rootviewcontroller,但没有在 logoutData
& 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()
}
成功登录后,尝试将 NavigationController 设置为 rootViewController 而不是您的 ViewController
您的后退按钮将开始出现。
在 AppDelegate 中,在 else 块中,而不是这一行
让主页=mainStoryBoard.instantiateViewController(withIdentifier:"ViewController")为! ViewController
写这个
让主页 = mainStoryBoard.instantiateViewController(withIdentifier: "NavigationController") 为! UINavigationController
在登录内部ViewController,在 if(message == 1)
块中替换
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
和
let navController:UINavigationController = self.storyboard!.instantiateViewController(withIdentifier: "NavigationController") 作为! UINavigationController
还将故事板中 UINavigationController 的故事板标识符设置为 NavigationController
从 Home.swift 中删除此内容,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
因为它没有继承导航控制器的属性
并将其添加到 Appdelegate.swift 文件
对于其他3个视图控制器,您需要在每个Segway之间添加导航控制器以便继承它或通过分别实例化视图控制器来编码按钮