首页 文章

Swift - 导航视图控制器不出现 Headers

提问于
浏览
7

我有一个导航视图控制器,在操作时将一个segue发送到标签栏视图控制器 . 由于此segue,选项卡式视图控制器继承导航栏 . 我正在尝试将 Headers 应用于附加到标签栏视图控制器的我的一个视图控制器,但是通过代码设置 Headers 对我来说不起作用 . 有谁知道原因可能是什么?

这是我的故事板的图片:

storuboard

带有注销按钮的视图控制器是我试图在导航栏(代码)中设置 Headers 的地方:

import UIKit

class ProfileSettingsViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {

        self.navigationItem.title = "Profile Settings"

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.leftBarButtonItem = nil


    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func logoutButton(sender: AnyObject) {

        PFUser.logOut()
        var currentUser = PFUser.currentUser()
        self.performSegueWithIdentifier("userLoggedOut", sender: self)
        self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

    }

}

嵌入导航控制器的视图控制器触发到标签栏控制器的segue:

import UIKit

class UserRegistrationViewController: UIViewController {


    func displayAlert(title:String, error:String) {

        var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
            action in



        }))

        self.presentViewController(alert, animated: true, completion: nil)


    }

    @IBOutlet var usernameTextField: UITextField!

    @IBOutlet var emailTextField: UITextField!

    @IBOutlet var passwordTextField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func registerUser(sender: AnyObject) {

        var error = ""

        if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil {

            error = "Please enter a username, email and password"

        }


        if error != "" {

            displayAlert("Error In Form", error: error)

        } else {

            var user = PFUser.currentUser()

            user.username = usernameTextField.text
            user.password = passwordTextField.text
            user.email = emailTextField.text

            user.saveInBackgroundWithBlock {
                (succeeded: Bool!, signupError: NSError!) -> Void in
                if signupError == nil {

                    println(user.username)
                    println(user.password)
                    println(user.email)


                    self.performSegueWithIdentifier("successfulRegistration", sender: self)

                    self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)

                    // Hooray! Let them use the app now.
                } else {

                    if let errorString = signupError.userInfo?["error"] as? NSString {
                        error = errorString
                    } else {

                        error = "Please try again later."

                    }


                    self.displayAlert("Could Not Sign Up", error: error)

                }
            }


        }


    }



}

5 回答

  • 9

    这对我有用: self.parent?.title = "nav bar title"

  • 1

    在视图控制器层次结构中,导航栏显示 UITabBarController 的 Headers ,而不是 UITabBarController 中的视图控制器 .

    在导航栏中显示 Headers 的最简单方法是

    override func viewWillAppear(animated: Bool) {
    
        self.tabBarController?.navigationItem.title = "Profile Settings"
    
    }
    
  • 2

    在Logout Screen viewcontroller中添加以下内容 -

    self.tabBarController?.navigationItem.title="Profile Settings"
    
  • 4

    SWIFT 3

    如果嵌入在UINavigationController中,您可以在ViewController的viewDidLoad()方法中设置如下 Headers .

    override func viewDidLoad() {
        super.viewDidLoad()
    
        /* If view controller is a direct child of UINavigationController */
        self.title = "Title Here"
    
        /* If view controller's parent is a direct child of UINavigationController e.g. a child of embedded tab view controller */
        self.parent?.title = "Title Here" 
    }
    
  • 10

    试试这个:

    self.navigationController?.navigationBar.topItem?.title = "Profile Settings"
    

相关问题