Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOS Swift如何让枚举返回不同的类_Swift_Enums_Swift4 - Fatal编程技术网

IOS Swift如何让枚举返回不同的类

IOS Swift如何让枚举返回不同的类,swift,enums,swift4,Swift,Enums,Swift4,我想知道是否有一种方法可以拥有一个枚举,并从不同的类中为它分配不同的值例如,我有3个类实例化一个视图控制器,我总是这样启动它们 let placeAreaC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PlaceAreaC") as! PlaceAreaC let notificationC = UIStoryboard(name

我想知道是否有一种方法可以拥有一个枚举,并从不同的类中为它分配不同的值例如,我有3个类实例化一个视图控制器,我总是这样启动它们

let placeAreaC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PlaceAreaC") as! PlaceAreaC

let notificationC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC

let reportC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReportC") as! ReportC
每次我想去一个新的控制器,我必须这样做,我想知道我是否可以创建一个枚举,可以保存这些值,现在我有这个代码

class ControllerEnum: NSObject {
    
    enum InstantiateEnum: String {
        case Report = "Report" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReportC") as! ReportC
        case Notification = "Notification" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC
        case PlaceArea = "PlaceArea" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReportC") as! ReportC
        
    }
  }
我知道类型是String,但我想知道如何使它能够接受上述3个不同的类?这样我就可以参考他们做的事情

实例化紧急通知


这将非常有用,因为现在我可以在一个地方更改这些值。任何建议都很好。

您可以在枚举中拥有返回ViewController的属性

enum InstantiateEnum: String {
    case Report = "Report" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReportC") as! ReportC
    case Notification = "Notification" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationC") as! NotificationC
    case PlaceArea = "PlaceArea" // UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReportC") as! ReportC

   var viewController:UIViewController {
       switch self  {
           case  Report :
             return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: self.rawValue)
           ...

       }

   }

}

在每个视图控制器返回自对象中可以有一个类方法

并创建标识符的常量

struct StoryBoard {

    static let main = UIStoryboard.init(name: "Main", bundle: nil)
    static let otherStoryboard = UIStoryboard.init(name: "otherStoryboard", bundle: nil)

    struct controller {
        static let ReportC                                  =   "Report"
     }
}

我使用了更好的方法:

步骤1:只需复制带有扩展名的枚举即可。并将故事板名称更改为项目中的名称

enum AppStoryboard : String {

case Main
case Notification
case Profile
case Login
}

extension AppStoryboard {

var instance : UIStoryboard {

    return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
}

func viewController<T : UIViewController>(viewControllerClass : T.Type, function : String = #function, line : Int = #line, file : String = #file) -> T {

    let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID

    guard let scene = instance.instantiateViewController(withIdentifier: storyboardID) as? T else {

        fatalError("ViewController with identifier \(storyboardID), not found in \(self.rawValue) Storyboard.\nFile : \(file) \nLine Number : \(line) \nFunction : \(function)")
    }

    return scene
}

func initialViewController() -> UIViewController? {

    return instance.instantiateInitialViewController()
}
}

extension UIViewController {

  // Not using static as it wont be possible to override to provide custom storyboardID then
class var storyboardID : String {

    return "\(self)"
}

static func instantiate(fromAppStoryboard appStoryboard: AppStoryboard) -> Self {

    return appStoryboard.viewController(viewControllerClass: self)
} }
枚举AppStoryboard:字符串{ 主箱 个案通知 病例资料 案例登录 } 扩展脚本脚本{ var实例:UIStoryboard{ 返回UIStoryboard(名称:self.rawValue,bundle:bundle.main) } func viewController(viewControllerClass:T.Type,function:String=#function,line:Int=#line,file:String=#file)->T{ 让故事板ID=(viewControllerClass作为UIViewController.Type)。故事板ID guard let scene=instance.InstanceEviewController(标识符:storyboardID)为?T else{ fatalError(“标识符为\(故事板ID)的ViewController,未在\(self.rawValue)故事板中找到。\n文件:\(文件)\n行号:\(行)\n函数:\(函数)”) } 返场 } func initialViewController()->UIViewController{ return instance.instanceInitialViewController() } } 扩展UIViewController{ //不使用静态,因为无法覆盖以提供自定义情节提要ID 类变量storyboardID:String{ 返回“\(self)” } 静态func实例化(来自appStoryboard appStoryboard:appStoryboard)->Self{ 返回appStoryboard.viewController(viewControllerClass:self) } } 第二步: 指定与序列图像板中视图控制器的类名相同的序列图像板id

第三步: 现在创建viewcontroller的对象,如下所示:

let notificationVC = NotificationVC.instantiate(fromAppStoryboard: .Notification)

/* Syntax:
let <yourObject> = <YourViewController's class name>.instantiate(fromAppStoryboard: <respected storyboard>) */
让notificationVC=notificationVC.instantiate(来自AppStoryboard:.通知)
/*语法:

let=我认为您正在寻找类似的内容请检查可能不太清楚您所追求的是什么(?),但您可以在枚举中使用函数或变量返回适当的viewController,留给您的api如下:
InstantiateEnum.Report.viewController
想想看,为什么您实际上需要枚举?听起来您基本上想要的是一个经典的工厂(?)让storyboardID=(viewControllerClass作为UIViewController.Type)。不能在swift中的类型“UIViewController”上使用将错误显示为实例成员“storyboard”的故事板5@ashinasok我已更新步骤1代码,请使用新代码重试。不要将“storyboardID”更改为“storyboardID”。没有storyboardID,因为类型“UIViewController”没有成员“storyboardID”,所以出现错误;你是说“故事板”吗?@ashinasok我在上面的代码中添加了一个类var作为故事板id。检查步骤1中的代码。
let notificationVC = NotificationVC.instantiate(fromAppStoryboard: .Notification)

/* Syntax:
let <yourObject> = <YourViewController's class name>.instantiate(fromAppStoryboard: <respected storyboard>) */