Ios 如何使此按钮以编程方式打开新的导航控制器?
如何使此按钮打开新的导航控制器?我想让它从右边强制打开一个新的控制器 我需要以编程方式完成这一切,而不是使用故事板Ios 如何使此按钮以编程方式打开新的导航控制器?,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,如何使此按钮打开新的导航控制器?我想让它从右边强制打开一个新的控制器 我需要以编程方式完成这一切,而不是使用故事板 @objc func buttonAction(sender: UIButton!) { let loginDetailController = UIViewController() navigationController?.pushViewController(loginDetailController, animated: true) print("B
@objc func buttonAction(sender: UIButton!) {
let loginDetailController = UIViewController()
navigationController?.pushViewController(loginDetailController, animated: true)
print("Button tapped")
}
下面是当用户未登录时,使我正在编辑的视图控制器弹出的代码。此代码位于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)
}
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)
}
}
}
这里是我添加导航控制器的位置
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)
}
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)
}
}
新方法
故事板
首先创建一个自定义viewController类的新文件。对于本例,我们将其称为YourCustomViewController。然后转到情节提要并向情节提要添加新的viewController。选择该ViewController并为其指定ID并设置其类。完成后,将以下代码放入函数中:
let controller = self.storyboard!.instantiateViewController(withIdentifier: "Your View's Identifier") as! YourCustomViewController
self.navigationController!.pushViewController(controller, animated: true)
没有故事板:
在AppDelegate中
在您的行动中呈现视图
经过讨论,了解更多情况。这就是在新NavigationController中以编程方式显示ViewController的方式 然后,要在最后销毁导航控制器,应该如下所示:
self.navigationController?.dismiss(animated: true) {
//
}
当登录完成时,当您需要转到下一个视图时,请执行此操作。我实际上是以编程方式编写所有内容,因此我没有故事板。如何以编程方式获得相同的结果?我认为没有故事板很好。你应该确认这家伙是一个不可思议的资源。我真的很感谢你的帮助,但当我点击按钮时,什么都没有发生。有什么想法吗?在AppDelegate中将rootViewController设置为UINavigationController吗?好的,现在loginController在尝试登录后无法关闭。我认为这是因为您将其设置为根ViewControllert,这是您希望嵌入到NavigationController中的控件,对吗?是的,导航控制器需要是登录控制器的一部分,然后在登录控制器被解除后解除。不完全是这样。我编辑了这篇文章,所以你可以看到我在哪里添加了导航。它仍然不是解雇删除第一次解雇
@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)
}
self.navigationController?.dismiss(animated: true) {
//
}