我有这个问题:
我有一个名为CoredataAction
的自定义类
在这个类中,我执行了所有 CoreData 操作,但它不是 UIViewController
。
如何从我的 CoreDataAction
类中打开 View ?
我尝试打开 Storyboard,但没有成功!它给我带来了 bad_access 错误
最佳答案
一种解决方案是在保存初始 ViewController
的 CoredataAction
类中添加一个变量,只需确保在初始化 CoredataAction 时设置该变量即可
类。
CoredataAction
class CoredataAction {
var parentViewController:UIViewController!
func presentNewViewController() {
let newViewController = parentViewController.storyboard?.instantiateViewControllerWithIdentifier("YOUR STORYBOARD ID") as UIViewController
parentViewController.presentViewController(newViewController, animated: true, completion: nil)
}
}
View Controller
func initCustomClass() {
var coreData = CoredataAction()
coreData.parentViewController = self
}
另一种选择是使用协议(protocol)将 View 呈现委托(delegate)回 ViewController 类本身。这将是与上面非常相似的设置,它只是意味着 View 呈现逻辑可以保留在您的 CoredataAction
自定义类之外 - 如果您需要这样的示例,请告诉我。
关于ios - 从自定义类 Swift IOS 打开 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106127/