gpt4 book ai didi

ios - 在 UINavigationBar 上设置导航项时崩溃

转载 作者:可可西里 更新时间:2023-11-01 00:36:29 34 4
gpt4 key购买 nike

我有一个嵌入在导航 Controller 中的 View Controller 。

在导航栏上,我在栏的中央有一个 segmentedControl,然后在右边有一个编辑按钮。

当我点击“编辑”按钮时,将触发以下代码:

func editAction() {

if homeworkTasksVC?.editing == false {

// Set to editing
homeworkTasksVC?.setEditing(true, animated: true)

// Switch to "Done" button and 'Homework' title without segmented control
let navBar = self.navigationController?.navigationBar
let navItem = UINavigationItem(title: "Homework")

let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(TasksParentViewController.editAction))
navItem.rightBarButtonItem = doneButton

navBar!.setItems([navItem], animated: true)


} else {

// Stop editing
homeworkTasksVC?.setEditing(false, animated: true)

// Switch back to "Edit" button and segmented control
let navBar = self.navigationController?.navigationBar
let navItem = UINavigationItem(title: "Homework")
navItem.titleView = segmentControl

let editButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Edit, target: self, action: #selector(TasksParentViewController.editAction))
navItem.rightBarButtonItem = editButton

navBar!.setItems([navItem], animated: true)

}

}

(注意:homeworkTasksVC 被引用是因为有问题的 View 是一个 UIPageViewController 而不是在两个 View 之间切换)

问题:当我点击编辑时,分段控件毫无问题地切换到文本标题,然后在再次点击“编辑”时切换回来,但是如果我点击view 导航到不同的 View ,然后点击“返回”返回原始 View ,然后点击编辑,它崩溃并显示以下输出:

2016-06-22 00:38:59.272 2.0[1504:386949] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call setItems:animated: directly on a UINavigationBar managed by a controller.' * First throw call stack: (0x23c85b0b 0x23442dff 0x23c85a51 0x283a0ced 0x118318 0x1189c8 0x2829b6a1 0x2841e0b9 0x2829b6a1 0x2829b631 0x2828356b 0x28283697 0x2829af51 0x2829abbf 0x28293547 0x28263dd5 0x28262379 0x23c479e7 0x23c475d7 0x23c4593f 0x23b941c9 0x23b93fbd 0x251b0af9 0x282cc435 0xe8cb4 0x23840873) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

最佳答案

您想要的是在当前 View Controller 上设置 navigationItem 属性。当您将第二个 View Controller 推送到导航堆栈时,UINavigationController 将接管导航栏项的管理。它实际上可能是一个错误,它在推送之前起作用。

代替:

navBar!.setItems([navItem], animated: true)

做任何一个:

homeworkTasksVC.navigationItem = navItem

或:

self.navigationItem = navItem

选择哪一个取决于您如何堆叠 View Controller 。

关于ios - 在 UINavigationBar 上设置导航项时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37947657/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com