[英]How can I make this button open a new navigation controller programmatically?
How can I make this button open a new navigation controller? 如何使此按钮打开新的导航控制器? I want it to force open a new controller from the right. 我希望它从右侧强制打开一个新的控制器。
I need to do this all programmatically not using the storyboard. 我需要以编程方式不使用情节提要板来完成所有操作。
@objc func buttonAction(sender: UIButton!) {
let loginDetailController = UIViewController()
navigationController?.pushViewController(loginDetailController, animated: true)
print("Button tapped")
}
Here is the code that makes the view controller I am editing pop up when a user is not logged in. This code is in the rootview controller. 这是使用户未登录时弹出正在编辑的视图控制器的代码。该代码位于rootview控制器中。
func checkIfUserIsLoggedIn() {
if Auth.auth().currentUser?.uid == nil {
perform(#selector(handleLogout), with: nil,
afterDelay: 0)
}else{
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.navigationItem.title = dictionary["name"] as? String
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}
},withCancel: nil)
}
}
override func didMove(toParentViewController parent: UIViewController?) {
checkIfUserIsLoggedIn()
}
@objc func handleLogout() {
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
let loginController = TwoLoginController()
present(loginController, animated: true, completion:
nil)
}
} }
Here is where I added the dismiss of navigation controller. 这是我添加了导航控制器关闭的地方。
func handleLogin() {
guard let email = emailTextField.text, let password = passwordTextField.text else{
print("invalid form")
return
}
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if error != nil {
print(error!)
return
}
//logged in
self.dismiss(animated: true, completion: nil)
self.navigationController?.dismiss(animated: true, completion: nil)
}
}
New Approach? 新的方法?
import UIKit
import Firebase
class TwoLoginController: UINavigationController {
With Storyboards 带情节提要
First create a new file that is a custom viewController class. 首先创建一个新文件,它是一个自定义viewController类。 For this example we will call it YourCustomViewController. 在此示例中,我们将其称为YourCustomViewController。 Then ( go to your storyboard and add a new viewController to the storyboard. Select that ViewController and give it an ID and set its class. After that is done put the following code in your function : 然后(转到故事板,然后在故事板上添加新的viewController。选择该ViewController并为其指定ID并设置其类。完成后,将以下代码放入函数中:
let controller = self.storyboard!.instantiateViewController(withIdentifier: "Your View's Identifier") as! YourCustomViewController
self.navigationController!.pushViewController(controller, animated: true)
Without Storyboards : 没有情节提要:
In your AppDelegate 在您的AppDelegate中
// put the following code in your appDelegate didFinishLaunchingWithOptions function
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))
UINavigationBar.appearance().barTintColor = UIColor.rgb(230, green: 32, blue: 31)
// get rid of black bar underneath navbar
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
application.statusBarStyle = .lightContent
let statusBarBackgroundView = UIView()
statusBarBackgroundView.backgroundColor = UIColor.rgb(194, green: 31, blue: 31)
window?.addSubview(statusBarBackgroundView)
window?.addConstraintsWithFormat("H:|[v0]|", views: statusBarBackgroundView)
window?.addConstraintsWithFormat("V:|[v0(20)]", views: statusBarBackgroundView)
In your Action to present the view 在您的动作中呈现视图
// this code belongs in your button's action
let dummyViewController = UIViewController()
dummyViewController.view.backgroundColor = .white
dummyViewController.navigationItem.title = "TEST"
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.pushViewController(dummyViewController, animated: true)
After a discussion and getting more of the picture. 经过讨论并获得更多了解。 This is how you should programmatically present a ViewController
inside of a new NavigationController
这是您应如何以编程方式在新NavigationController
内显示ViewController
的方法
@objc func handleLogout() {
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
let loginController = TwoLoginController()
let navVC = UINavigationController(rootViewController: loginController)
present(navVC, animated: true, completion: nil)
}
Then to destroy the navigation controller at the end it should be something like this: 然后,要在最后销毁导航控制器,应该是这样的:
self.navigationController?.dismiss(animated: true) {
//
}
Do this when the log in is completed when you need to segue to the next view. 当需要登录到下一个视图时,请在登录完成后执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.