简体   繁体   English

如何在保留数据的同时将UIViewController下转换为子类的子类呢?

[英]In swift how does one downcast a UIViewController to subclass of a subclass while retaining data?

Currently I'm working on a project and attempting to downcast from a viewcontroller instantiated from the storyboard using this method 目前,我正在一个项目上,并尝试使用此方法从情节提要中实例化的视图控制器进行向下投射

let cl = self.storyboard?.instantiateViewController(withIdentifier: "contact_list_view")
let contact_list_controller = cl as? ContactListViewController

And yes currently the identifier and the view controller type for the view is set in the storyboard. 是的,当前在情节提要中设置了视图的标识符和视图控制器类型。 However the problem I'm having is once it downcast the member variables don't persist. 但是,我遇到的问题是,一旦向下转换成员变量,它们就不会持续存在。

Right now: ContactListViewController inherits from UITableViewController and that inherits from UIViewController. 现在:ContactListViewController继承自UITableViewController,并且继承自UIViewController。 Is there some sort of issue downcasting in regards to multiple inheritance in an earlier project I was working on I didn't come across this issue using a single level inheritance (A to B instead of currently A to C). 在我正在从事的较早项目中,关于多重继承是否存在某种形式的问题down贬不一,我没有使用单级继承(A到B而不是当前的A到C)遇到此问题。

@IBAction func switchToContractViewForAdd(_ sender: Any) 
    isAdd_or_edit = true;string = "hello"   
    let contact_view=self.storyboard?.instantiateViewController(withIdentifier:"contact_view")
    self.navigation?.pushViewController(contact_view!, animated: true)
}

override func viewDidLoad() {
    super.viewDidLoad()
    let cl = (self.storyboard?.instantiateViewController(withIdentifier: "contact_list_view"))
    contact_list_controller = cl as? ContactListViewController
    contact_list_controller?.string ?? "default")
}

Alright so here is a snippet of my code basically whats happening is the first function is acting as a segue transition to show the "contact_view". 好了,这是我的代码的一个片段,基本上发生了什么事,这是第一个函数充当segue过渡来显示“ contact_view”。 Once the contact view is loaded I want to access some of the values from the previous view "contact_list_view" (for simplicity purposes I added a string variable called string with a default value of "hi" and on the first function call I change it "hello" before performing the segue). 加载联系人视图后,我要访问上一个视图“ contact_list_view”中的某些值(为简单起见,我添加了一个名为string的字符串变量,默认值为“ hi”,在第一个函数调用中我将其更改为“打招呼之前)。 After attempting to downcast the the "contact_list_view" when retrieving it from the storyboard the value of string is "hi" once again. 在从情节提要中检索“ contact_list_view”时,尝试向下转换后,字符串的值再次为“ hi”。

The reason (well, part of the reason) you are not getting the changed value of "hello" is because: 您未获得更改的“ hello”值的原因(部分原因)是因为:

override func viewDidLoad() {
    super.viewDidLoad()
    let cl = (self.storyboard?.instantiateViewController(withIdentifier: "contact_list_view"))
    contact_list_controller = cl as? ContactListViewController
    contact_list_controller?.string ?? "default")
}

instantiateViewController creates a new instance of the specified view controller each time you call it. instantiateViewController 每次调用时都会创建一个指定视图控制器的新实例。 So, no matter what else you are doing to it after that line, you will never get the value you were trying to set in switchToContractViewForAdd() 因此,无论在那行之后您对其进行了什么操作,您都永远不会获得在switchToContractViewForAdd()中尝试设置的值。

Instead, you need to look into "passing data between view controllers" (you'll find uncountable examples and explanations out there by simple searching). 相反,您需要研究“在视图控制器之间传递数据”(您将通过简单的搜索找到无数的示例和说明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM