navigationItem.leftBarButton is not appearing (Xcode Swift 4



我制作了一个UITableViewController(用于表视图的iOS控制器(,并试图在屏幕左上角放置一个注销按钮,代码为:

class ViewController: UITableViewController {
var logoutButton : UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
logoutButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
navigationItem.leftBarButtonItem = logoutButton
}

iOS模拟器只显示一个空(白色(表视图。我认为这将是一项非常简单的任务,不知道为什么它不起作用,也不知道从这里该去哪里。如有任何帮助,我们将不胜感激。

更新:我还尝试在UINavigationController和UIViewController中添加导航按钮;代码看起来像这个

class ViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let logoutButton = UIBarButtonItem(title: "Logout", style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = logoutButton
}

class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let logoutButton = UIBarButtonItem(title: "Logout", style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = logoutButton
}

我的AppDelegate如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}

什么都不管用。我找不到要显示的按钮。我不想使用故事板,但如果我连一个按钮都无法加载,我就没有其他选择了。感谢您的帮助。谢谢

问题出现在appDelegate中。rootViewController应该设置为:

window?.rootViewController = UINavigationViewController(rootViewController: UITableViewController())

与相反

window?.rootViewController = UITableViewController()

我认为这正是评论试图指出的;谢谢

在这种情况下,您必须在注销时再次推送loginViewController,而需要弹出ToRootViewControllerAnimated代码在注销按钮上使用此代码,单击Evnet:

[self.navigationController popToRootViewControllerAnimated:YES];

如果你使用的是swift,那么使用这个代码

self.navigationController?.popToRootViewControllerAnimated(true)

最新更新