ios - 从自定义类 Swift IOS 打开 View

标签 ios swift uiviewcontroller storyboard

我有这个问题:

我有一个名为CoredataAction的自定义类

在这个类中,我执行了所有 CoreData 操作,但它不是 UIViewController

如何从我的 CoreDataAction 类中打开 View ?

我尝试打开 Storyboard,但没有成功!它给我带来了 bad_access 错误

最佳答案

一种解决方案是在保存初始 ViewControllerCoredataAction 类中添加一个变量,只需确保在初始化 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/

相关文章:

iphone - 屏幕上的多个 ViewController

ios - 如何在 iOS 中使用自定义转换解决此问题?

ios - 根据iPhone屏幕大小显示不同的xib文件

ios - 是否可以将 2 个 segues 附加到指向两个不同 UIViewController 的 UITableViewCell?

ios - ALAssets 的 NSPredicate 和 NSArray

ios - 从文本字段输入值到 NSArray

ios - 如何从 URL 异步下载图像

ios - 在 Swift 3.0 中使用 XMPP 阻止和取消阻止用户

StackView 中的 iOS UIButtons 没有被点击

swift - 重用 Storyboard View Controller - 父/ subview Controller