[英]How to navigate to another viewcontroller with PUSH style from UITabBarController page
[英]How to navigate UITabBarController to ViewController?
我里面有一個TabBarController
,我正在加載兩個ViewControllers
。
import UIKit
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
圖片:
class AttendanceViewController: UIViewController,UIApplicationDelegate,
UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate,
CLLocationManagerDelegate,UIImagePickerControllerDelegate {
override func viewWillAppear(_ animated: Bool) {
if Auth.auth().currentUser?.uid == nil {
//Below code is not working
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(vc!, animated: true)
print("user is not login in redirect to login page")
return
}else{
print("User Successfully loginedin")
}
}
}
注意:我有兩個像AttendanceViewController和TaskViewController這樣的視圖控制器,我想檢查用戶是否未登錄,然后我想重定向到我的LoginViewController,我嘗試了上面的代碼,但它不起作用
如果您的ViewController嵌入在UINavigationController
,則您編寫的代碼將起作用。 因此,除了顯示不存在的UINavigationController
中的UIViewController
,您還可以顯示LoginViewController
。 因此,替換為:
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
self.navigationController?.pushViewController(vc!, animated: true)
與
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main)
.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
present(vc!, animated: true, completion: nil)
或者,您可以將UIViewController
嵌入UINavigationController
並設置帶有標識符的segue,然后對LoginViewController
執行此segue。
7.進行檢查
performSegue(withIdentifier: "YourIdentifier", sender: self)
嘿,我剛剛創建了一個示例,它將幫助您:-
我只是使用userdefaults
保存loginState
這是:-
1)。 TaskViewController
我剛剛在其中創建了一個退出按鈕
import UIKit
class TaskViewController: UIViewController {
@IBOutlet weak var signOutButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
signOutButton.layer.cornerRadius = signOutButton.layer.frame.height/2
}
@IBAction func signOutButtonTapped(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "isUserLoggedIn") // here I remove the boolean value
UserDefaults.standard.synchronize()
let storyBoard = UIStoryboard(name: "Login", bundle: nil)
let loginVc = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let rootVc = UINavigationController(rootViewController: loginVc)
UIApplication.shared.keyWindow?.rootViewController = rootVc
}
}
2)。 AttendanceViewController-我沒有在其中創建任何內容
import UIKit
class AttendanceViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
這兩個控制器都在TabBarController
內部,后者是HomeController
import UIKit
class HomeController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
並在login.storyboard
文件中創建LoginViewController
LoginViewController:-這里保存了loginState
import UIKit
class LoginViewController: UIViewController {
@IBOutlet weak var loginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
loginButton.layer.cornerRadius = loginButton.layer.frame.height/2
}
@IBAction func loginButtonTapped(_ sender: Any) {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "HomeController") as! HomeController
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.synchronize()
UIApplication.shared.keyWindow?.rootViewController = viewController
}
}
最后在Appdelegate.swift
檢查didFinishLaunchingWithOptions
的loginState像這樣:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let userLoginStatus = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
print(userLoginStatus)
if(userLoginStatus)
{
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "HomeController") as! HomeController
window!.rootViewController = centerVC
window!.makeKeyAndVisible()
} else {
let mainStoryBoard = UIStoryboard(name: "Login", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
window!.rootViewController = centerVC
window!.makeKeyAndVisible()
}
return true
}
這是完整的項目-https://www.dropbox.com/s/3d34raovyx6orda/saveLogin.zip?dl=0
我認為您想在appDelegate
進行檢查。 如今,我還與UITabbarcontroller
一起使用。我建議設置一個標簽(使用userDefaults)來檢查用戶是否登錄。 我的代碼是
class AppDelegate: UIResponder, UIApplicationDelegate , UITabBarControllerDelegate , GIDSignInDelegate{
var window: UIWindow?
var userCurrentLocation : CLLocation?
var mapView : GMSMapView?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if UserDefaults.standard.value(forKey: "LoginStatus")as? String == "1"
{
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let initailVC = storyBoard.instantiateViewController(withIdentifier: "tabBar")
let navVC = UINavigationController(rootViewController: initailVC)
self.window?.rootViewController = navVC
}
return true
}
我正在使用此代碼來檢查用戶是否正在登錄,它將重定向到TabBarController
,否則將保留在LoginVC中。
將您的UITabBarController
嵌入到UINavigationController
UINavigationController(rootViewController: UITabBarController(nibName: "TabBarNibName", bundle: Bundle.main))
只需從TabBarController的導航控制器中推送
tabbarcontroller.navigationController?.pushViewController(vc!, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.