[英]Presenting View Controller loses subviews when dismissing presented VC
我在使用兩個以直接方式交互的視圖控制器時遇到了一些麻煩:
homeViewController顯示一個待辦事項列表以及一個addTask按鈕。 addTask按鈕將啟動一個附加的viewController,充當用戶填寫的“表單”。
但是,致電
self.dismissViewControllerAnimated(true, completion: nil);
在顯示的視圖控制器中,我返回到主頁,但該頁面為空白白色,除了可以在情節提要板上看到最高級別的視圖(即覆蓋整個屏幕的視圖)以外,什么都看不到。
我所有的視圖,場景等都是通過情節提要中的自動布局設置的。 我四處查看Stack Overflow,這導致我玩弄自動調整大小的子視圖參數,即:
self.view.autoresizesSubviews = false;
無濟於事。 我是在錯誤地調整了自動調整大小參數(在感興趣的錯誤視圖中,或者只是將其設置為錯誤),或者在解決其他問題。
提前致謝
編輯:
我介紹的VC如下:
func initAddNewTaskController(){
let addNewTaskVC = self.storyboard?.instantiateViewControllerWithIdentifier("AddNewTaskViewController") as! AddNewTaskViewController;
self.presentViewController(addNewTaskVC, animated: true, completion: nil);
}
編輯2:
盡管我接受使用委托或消除混亂確實可以繞開我遇到的問題(正如campbell_souped所建議的那樣),但我仍然不理解當我關閉導致空白屏幕的視圖控制器時發生的根本問題。
我知道調用dismissViewControllerAnimated會傳遞到當前的視圖控制器(在本例中為homeViewController)。 由於我不需要進行任何解雇之前或之后的配置,因此在我看來(在我看來)不需要使用委托。
我目前的想法是由於某種原因,當我調用
dismissViewControllerAnimated(true, completion:nil);
在我的addNewTaskViewController中,它實際上是在釋放我的homeViewController。 我希望有人能啟發我,因為我不了解視圖控制器的顯示/關閉方式到底是什么。
在這種情況下,我通常會選擇兩條路線之一。 在AddNewTaskViewController
上設置一個委托,或者使用展開AddNewTaskViewController
。
使用委托方法,設置一個協議:
protocol AddNewTaskViewControllerDelegate {
func didDismissNewTaskViewControllerWithSuccess(success: Bool)
}
在您的AddNewTaskViewController
添加一個代表代理的可選屬性
var delegate: AddNewTaskViewControllerDelegate?
然后調用didDismissNewTaskViewControllerWithSuccess
無論你什么時候解雇AddNewTaskViewController
:
如果記錄添加成功:
self.delegate?.didDismissNewTaskViewControllerWithSuccess(true)
self.dismissViewControllerAnimated(true, completion: nil);
或如果取消/失敗:
self.delegate?.didDismissNewTaskViewControllerWithSuccess(false)
self.dismissViewControllerAnimated(true, completion: nil);
最后,將您自己設置為委托,修改您之前的代碼段:
func initAddNewTaskController(){
let addNewTaskVC = self.storyboard?.instantiateViewControllerWithIdentifier("AddNewTaskViewController") as! AddNewTaskViewController;
self.presentViewController(addNewTaskVC, animated: true, completion: nil);
}
對此:
func initAddNewTaskController() {
guard let addNewTaskVC = self.storyboard?.instantiateViewControllerWithIdentifier("AddNewTaskViewController") as AddNewTaskViewController else { return }
addNewTaskVC.delegate = self
self.presentViewController(addNewTaskVC, animated: true, completion: nil);
}
...
}
// MARK: AddNewTaskViewControllerDelegate
extension homeViewController: AddNewTaskViewControllerDelegate {
func didDismissNewTaskViewControllerWithSuccess(success: Bool) {
if success {
self.tableView.reloadData()
}
}
}
[擴展名在homeViewController
類之外的homeViewController
]
使用輕松的搜索方法,請看以下Ray Wenderlich示例: http ://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
這種方法涉及Ctrl鍵從您的IBAction拖動到視圖控制器上方的退出對象,然后從彈出菜單中選擇正確的操作名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.