如何使该按钮以编程方式打开新的导航控制器?

时间:2018-06-23 21:16:21

标签: ios swift uinavigationcontroller

如何使此按钮打开新的导航控制器?我希望它强制从右侧打开一个新控制器。

我需要以编程方式不使用情节提要来完成所有操作。

@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)
    }

}

新方法?

import UIKit
import Firebase

class TwoLoginController: UINavigationController {

2 个答案:

答案 0 :(得分:1)

  

使用情节提要

首先创建一个新文件,该文件是自定义viewController类。在此示例中,我们将其称为YourCustomViewController。然后((转到您的情节提要,向情节提要中添加一个新的viewController。选择该ViewController并为其指定ID并设置其类。完成后,将以下代码放入您的函数中:

$sub = Sales::select(DB::raw('field1,field3,SUM(field2)as total'))         
        ->groupBy('field1',DB::raw('field3 having total<> 0'));

YourMainModel::query()
->select('field1', 'field3','total')
->from(DB::raw("({$sub->toSql()}) as sub"))
->mergeBindings($sub->getQuery())
->groupBy('field1')
->get();
  

没有情节提要:

在您的AppDelegate中

let controller = self.storyboard!.instantiateViewController(withIdentifier: "Your View's Identifier") as! YourCustomViewController
self.navigationController!.pushViewController(controller, animated: true)

在您的操作中显示视图

// 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)

答案 1 :(得分:0)

经过讨论并获得更多图片。这是您应如何以编程方式在新的if (cadena.matches("[0-9],[0-9]")){ Do something} //this being like if the string has numbers before and after the comma 内展示cadena = "2,2345"

ViewController

然后最后要破坏导航控制器,它应该是这样的:

NavigationController

需要登录到下一个视图时,在登录完成后执行此操作。