2017-02-14 80 views
0

我提出一個模式視圖控制器中,用戶可以輸入一些信息,然後在保存有此功能的信息......錯誤時重新呈現視圖控制器

func handleSave() { 

    guard let newProductUrl = NSURL(string: urlTextField.text!) else { 
     print("error getting text from product url field") 
     return 
    } 
    guard let newProductName = self.nameTextField.text else { 
     print("error getting text from product name field") 
     return 
    } 
    guard let newProductImage = self.logoTextField.text else { 
     print("error getting text from product logo field") 
     return 
    } 

    DispatchQueue.main.async { 
     self.productController?.save(name: newProductName, url: newProductUrl as URL, image: newProductImage) 
    } 


    // Present reloaded view controller with new product added 
    let ac = UINavigationController() 
    let pController = ProductController() 
    productController = pController 
    ac.viewControllers = [pController] 
    present(ac, animated: true, completion: nil) 

} 

...我得到一個錯誤在ProductControllerviewWillAppear(該控制器提供的模態視圖控制器,現在正試圖要回)

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    guard let appDelegate = 
     UIApplication.shared.delegate as? AppDelegate else { 
      return 
    } 

    let managedContext = 
     appDelegate.persistentContainer.viewContext 

    let companyToDisplay = self.navigationItem.title! 

    let fetchRequest = 
     NSFetchRequest<NSManagedObject>(entityName: "Product") 

    fetchRequest.predicate = NSPredicate(format:"company.name == %@",companyToDisplay) 

    do { 
     products = try managedContext.fetch(fetchRequest) 
     print(products) 
    } catch let error as NSError { 
     print("Could not fetch. \(error), \(error.userInfo)") 
    } 
} 

的錯誤是:意外地發現零,同時展開一個可選的,就行了。我如何指定它正在查找(和丟失)的self.navigationItem.title是發送模態視圖的控制器的self.navigationItem.title

感謝您的任何幫助,我一直在試圖將這個問題排除幾天,無法弄清楚。

編輯:這是我從我的ProductController

func presentModalView() { 

    let nc = UINavigationController() 
    let addProductController = AddProductController() 
    nc.viewControllers = [addProductController] 

    self.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
    self.modalPresentationStyle = .currentContext 
    self.present(nc, animated: true, completion: nil) 
} 

EDIT呈現模式的看法AddProductController:把代碼調度塊中:

DispatchQueue.main.async { 
     self.productController?.save(name: newProductName, url: newProductUrl, image: newProductImage) 

     let pController = ProductController() 
     self.productController = pController 
     self.navigationController?.pushViewController(pController, animated: true) 
    } 

回答

0

這是你的問題,你使用現在(AC ,animated:true,completion:nil)到這個視圖,所以這個演示文稿是模態的,沒有navigationController。 你必須使用UINavigationController.pushViewController來呈現視圖,這種方式你將得到navigationController。

編輯:

 let nc = UINavigationController() 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let addProductController = storyboard.instantiateViewController(withIdentifier: "addProductVC") 
    nc.viewControllers = [addProductController] 

    self.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
    self.modalPresentationStyle = .currentContext 
    self.present(nc, animated: true, completion: nil) 

只是不要忘記設置標識符addProductController在故事板,改變故事板和VC的標識你的。

編輯2:

  let nc = UINavigationController() 
    let addProductController = ProductController() 
    addProductController.navigationItem.title = "Have a good one" 
    nc.viewControllers = [addProductController] 

    self.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
    self.modalPresentationStyle = .currentContext 
    self.present(nc, animated: true, completion: nil) 
+0

我已將它更改爲'ac。pushViewController(P控制器,動畫:真)',我現在得到一個崩潰,出現錯誤:「推不止一次同樣的看法控制器實例不支持」 – d0xi45

+0

你不應該只是改變你的代碼一兩件事,改變交流到UINavigationController的,如果它沒有工作,請告訴我編輯自己的帖子,並把完整的代碼 –

+0

我不知道我理解你的要求 - 'UINavigationController.pushViewController(P控制器,動畫:真)'不起作用。 – d0xi45

0

的問題是,這裏面調用塊:

DispatchQueue.main.async { 
    self.productController?.save(name: newProductName, url: newProductUrl as URL, image: newProductImage) 
} 

實際執行您的handleSave()方法返回。看起來你期望它在該方法中相對於其他代碼順序執行。

DispathQueue.main.async將塊添加到未來某個時刻要執行的代碼隊列中 - 它不會立即執行。

爲了解決這個問題,你需要把代碼放在的調度塊中,它可以做任何下一步需要做的事情。你在這裏這將類似於:

// Present reloaded view controller with new product added 
let ac = UINavigationController() 
let pController = ProductController() 
productController = pController 
ac.viewControllers = [pController] 
present(ac, animated: true, completion: nil) 

但你可能要收拾你如何/創建和解聘視圖控制器在那裏 - 你有什麼在這裏看起來像它會不必要地堆了一堆的視圖控制器。

+0

是的,這就是我遇到的麻煩..我試圖把代碼放在調度塊內,但仍然得到了我的問題中提到的同樣的錯誤。我編輯我的問題,展現給我嘗試新的代碼(仍然習慣瀏覽各地的意見,以及核心數據,所以請讓我知道如果有一個更好的方式來做到這一點) – d0xi45

+0

這很難說,你要什麼不必查看所有代碼,但可以嘗試重新構造問題,以便僅使用ProductController的一個實例。看起來你不需要每次保存一些東西時創建一個新的。 –

+0

我知道,對不起,這對我來說是一個很大的學習項目,我迷失在視圖控制器/導航控制器中。我以爲我只有一個ProductController實例,然後是模態視圖'AddProductController'。我真的很困惑,不能真正解釋......如果你想看到錯誤,點擊這個項目(點擊編輯 - >完成 - >點擊一家公司,然後嘗試添加產品以查看錯誤):https ://dl.dropboxusercontent.com/u/17411581/NavCtrl.zip – d0xi45

相關問題