navigationItem.leftBarButton没有出现(Xcode Swift 4 IOS)

时间:2018-11-08 07:49:40

标签: ios swift swift4 uibarbuttonitem

我制作了一个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
}

没有任何效果。我无法显示按钮。我不想使用情节提要,但如果我什至没有按钮可以加载,那么我还有其他选择。任何帮助表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

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

public class RecordTimeStampExtractor implements TimestampExtractor {

    //default timestamp extractor
    private FailOnInvalidTimestamp failOnInvalidTimestamp = new FailOnInvalidTimestamp();

    @Override
    public long extract(ConsumerRecord<Object, Object> consumerRecord, long l) {
        //could also use consumerRecord.topic().equals("mytopic")
        if(consumerRecord.value() instanceof MyClass) {
            MyClass myClass = (MyClass) consumerRecord.value();
            return myClass.getRecordTimestamp().toEpochMilli();
        }
        return failOnInvalidTimestamp.extract(consumerRecord,l);
    }
}

相对于:

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

我认为这就是评论要指出的内容;谢谢!

答案 1 :(得分:-3)

在这种情况下,您必须在注销时再次推送loginViewController,而您需要popToRootViewControllerAnimated代码 在退出按钮clickEvnet上使用此代码:

const chunkArray = <T>(inputArray: T[], chunks: number) => {
    const chunkedArray: T[][] = []; // use T as the type for chunkedArray
    let i = 0;
    const n = inputArray.length;

    while (i < n) {
        chunkedArray.push(inputArray.slice(i, (i += chunks)));
    }

    return chunkedArray;
};

const chunkedArrays = chunkArray([1, 2, 3, 4, 5], 2); // number[][]
const chunkedArraysString = chunkArray(["1", "2", "3", "4", "5"], 2); // string[][]

如果您使用的是swift,请使用此代码

[self.navigationController popToRootViewControllerAnimated:YES];