2015-06-28 25 views
3

后,我在我的应用程序遇到一个很奇怪的错误,两种观点之间,self.navigationController变得nil斯威夫特 - self.navigationController变成零过渡

我有几个视图控制器:MainViewControllerSecondViewControllerPastSessionsViewControllerJournalViewController。我使用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) 
} 

感谢您抽空看看

+1

使用'self.navigationController .pushViewController(editJournalVC,动画:真)?',而不是'self.navigationController .presentViewController( editJournalVC,animated:true,completion:nil)' –

+0

为什么?我使用'present'而不是'push',因为我希望它以模态方式呈现 –

+1

OK,那么在'func cancelEntryOrEditAndReturn'中使用'self.dismissViewControllerAnimated(true,completion:nil)'而不是pop。与push不同,在当前视图控制器之上呈现模态视图控制器。它不在导航视图控制器堆栈中。 –

回答

6

你应该推而不是,如果你希望它是在相同的导航堆栈呈现editJournalVC。因为当你呈现控制器不再在相同的导航堆栈。另外,如果你正在展示它,你应该解雇它而不是弹出。所以,如果你想呈现控制器,你应该使用

self.dismissViewControllerAnimated(true, completion: nil) 

代替

self.navigationController?.popViewControllerAnimated(true)