- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在我的应用程序中遇到了一个非常奇怪的错误,在两个 View 之间,self.navigationController
正在变为 nil
我有几个 View Controller :MainViewController
、SecondViewController
PastSessionsViewController
、JournalViewController
。我使用 JournalViewController
有两个目的,将新条目保存到 CoreData 或编辑旧条目。详细信息与此错误并不相关。
当我尝试从堆栈中弹出 JournalViewController
并返回到 MainViewController
时发生错误,但 仅 JournalViewController
处于“编辑”模式,而不是处于“保存新输入模式”时
任何想法 1) 为什么会发生这种情况以及 2) 如何正确解决它以便我可以在编辑模式下从 JournalViewController
返回时返回到 PastSessionsViewController
?
这里有一些代码可以使事情具体化。
在 AppDelegate.swift
中(在 didFinishLaunchingWithOptions
内部):
navController = UINavigationController()
navController!.navigationBarHidden = true
var viewController = MainViewController()
navController!.pushViewController(viewController, animated: false)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor = UIColor.whiteColor()
window?.rootViewController = navController
window?.makeKeyAndVisible()
在 MainViewController
中:
func goToPastSessions(sender: UIButton) {
let pastSessionsVC = PastSessionsViewController()
self.navigationController?.pushViewController(pastSessionsVC, animated: true)
}
func goToWriteJournalEntry(sender: UIButton) {
let journalVC = JournalViewController(label: "Record your thoughts")
self.navigationController?.pushViewController(journalVC, animated: true)
}
在 PastSessionsViewController
中:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let editJournalVC = JournalViewController(label: "Edit your thoughts")
let indexPath = tableView.indexPathForSelectedRow()
let location = indexPath?.row
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! EntryCell
if let objects = pastSessionsDataSource.coreDataReturn {
if let location = location {
editJournalVC.journalEntryCoreDataLocation = location
editJournalVC.editEntry = true
editJournalVC.journalEntryToEdit = objects[location].journalEntry
}
}
self.navigationController?.presentViewController(editJournalVC, animated: true, completion: nil)
}
最后,在 JournalViewController
中:
func doneJournalEntry(sender: UIButton) {
journalEntryTextArea?.resignFirstResponder()
var entry = journalEntryTextArea?.text
if let entry = entry {
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "Session")
var error: NSError?
// new entry
if journalEntryText == "Record your thoughts" {
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Session] {
if let lastTime = objects.last {
lastTime.journalEntry = entry
}
}
} else {
// existing entry
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
request.sortDescriptors = [sortDescriptor]
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Session] {
var location = journalEntryCoreDataLocation
var object = objects[location!]
object.journalEntry = entry
}
}
if !managedObjectContext.save(&error) {
println("save failed: \(error?.localizedDescription)")
}
}
// in "edit" mode, self.navigationController is `nil` and this fails
// in "record a new entry" mode, it's not nil and works fine
self.navigationController?.popViewControllerAnimated(true)
}
func cancelEntryOrEditAndReturn(sender: UIButton) {
self.journalEntryTextArea?.resignFirstResponder()
// in "edit" mode, self.navigationController is `nil` and this fails
// in "record a new entry" mode, it's not nil and works fine
self.navigationController?.popViewControllerAnimated(true)
}
感谢观看
最佳答案
如果您希望它在同一个导航堆栈中,您应该推送 editJournalVC 而不是呈现。因为当您呈现 Controller 时,它不再位于同一个导航堆栈中。另外,如果您要展示它,则应该将其关闭而不是流行。所以如果你想呈现 Controller ,你应该使用
self.dismissViewControllerAnimated(true, completion: nil)
代替
self.navigationController?.popViewControllerAnimated(true)
关于ios - Swift – self.navigationController 在过渡后变为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105754/
您好,所以我有这个 storyboard 结构 我在 ViewController B 中有一个 button 我需要那个 button 指向底部NavigationController 其中有 Vi
我的基于 Storyboard 的应用具有以下结构: NavigationController (NavCa) —> CollectionViewController (VCa) —> 模态呈现 —>
我可以用 MyFirstAppDelegate *delegate = (MyFirstAppDelegate *)[[UIApplication sharedApplication] delegat
我正在使用以下策略构建一个基于窗口的(所有 UI 都以编程方式连接,没有 IB)MonoTouch 应用程序: 伪代码: Note: I'm not calling base.ViewDidLoad
我发现了困难的方法 navigationController.navigationBarHidden = NO; 和 navigationController.navigationBar.hidden
这两种说法有什么区别。为了隐藏导航栏,我使用以下语句之一在 viewWillAppear 方法中隐藏导航栏,如下所示: -(void)viewWillAppear:(BOOL)animated {
我已经发布了这个问题,但这次我发布了代码。所以我有一个uiviewController,在这个viewController的viewDidLoad中我隐藏了navigationController的b
我想在横向显示另一个没有选项卡栏的 View 。所以我创建了一个新 View ,只更改 self.view 和 self.hidesBottomBarWhenPushed = YES; 但这对我不起作
当我将新的 ViewController 推送到导航 Controller 堆栈上时,“后退”按钮是前一个 Controller 的标题。 如何将后退按钮中的文本更改为“后退”而不是默认的最后一个 C
我在我的应用程序中设置了一个 subview “弹出窗口”,如果用户点击 subview 弹出窗口上的按钮,我想显示一个 navController。到目前为止,我已经设置了按钮,但是如果我点击按钮,
我有两个问题: 问题 1:我正在尝试更改导航后退按钮,我正在从父 ViewController 执行此操作: -(void)prepareForSegue:(UIStoryboardSegue *)s
我有一个 UINavigationController,它的根 Controller (UITableViewController) 和一个 UIViewController 的实例,当我单击导航 C
我正在尝试制作我的第一个基于表格的应用程序。但是它不会改变导航栏上标题的颜色。 我试过这段代码,但它似乎不起作用: this.NavigationController.NavigationItem.T
问题如图所示。我怎样才能从 viewcontroller C 返回到 A。方法 popViewController Animated 不起作用。 最佳答案 您的 Controller 层次结构存在问题
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit th
我在使用 Navigation Controller 转到我的详细 View 时遇到问题。 我正在使用 tableview 来显示一些练习,当你点击练习时,你会进入详细 View ,嵌入在导航 Con
我试图通过允许用户添加算命应用程序的新结果来理解演示 View Controller 。当用户点击导航栏右上角的“添加新结果”时,会弹出一个新的 View Controller ,用户可以在此处输入新
当我点击页面底部的灰色按钮时,我想使用 popToRootController 从最后一页返回到第一页。 我不明白如何同时对两个导航 Controller 进行操作。我为最后两页使用了一个库。 Sid
我有一个 navigationController 嵌入在我的 Root View 和 subview 中,并且在我的一个 subview 中有: @IBAction func button(send
是否可以将后退按钮的图标从箭头更改为其他图标? (在导航栏中) 我在storyboard的preferences里没找到解决方案 最佳答案 如果您的部署目标至少是 iOS 7,您可以使用 backIn
我是一名优秀的程序员,十分优秀!