我有一个导航视图控制器,它在动作时向标签栏视图控制器发送一个序列。由于这个序列,标签栏视图控制器继承了导航栏。我试图将一个标题应用到标签栏视图控制器上的一个视图控制器上,但是通过代码设置标题对我不起作用。有人知道这是为什么吗?
下面是我的故事板图片:
带有注销按钮的视图控制器是我试图在导航栏中设置标题的地方(代码):
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)
}
}
视图控制器嵌入在导航控制器中,触发选项卡栏控制器的继续:
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)
}
}
}
}
}
8条答案
按热度按时间k0pti3hp1#
这对我很有效:
self.parent?.title = "nav bar title"
pvabu6sv2#
在视图控制器层次结构中,导航栏显示的是
UITabBarController
的标题,而不是UITabBarController
内部的视图控制器。在导航栏中显示标题最简单方法是
ryhaxcpt3#
试试这个:
6yjfywim4#
SWIFT 3
如果内嵌在UINavigationController中,您可以从ViewController的viewDidLoad()方法中设定标题,如下所示。
py49o6xq5#
在注销屏幕视图控制器中添加以下内容-
rm5edbpk6#
Swift 5简单的答案请遵循
yx2lnoni7#
检查注解行,了解ViewController(带和不带Tabbarcontroller)可用性
pjngdqdw8#
将以下代码添加到视图控制器中:
对我很有效。我希望这对某人有帮助。