2015-06-28 59 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)