我想知道是否有一种方法可以使用枚举并为其分配不同类的不同值,例如,我有3个实例化View Controller的类,我总是像这样启动它们
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个不同的类?那么我就可以像
那样引用它们了InstantiateEnum.Notification
这将非常有用,因为现在我可以在一个地方更改这些值。任何建议都会很棒。
答案 0 :(得分:0)
您可以拥有在枚举
中返回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)
...
}
}
}
OR
您可以在每个View Controller返回自我对象中使用一个类方法
喜欢
//--------------------------------------------------------------------------------
// MARK: - Abstract Methods
//--------------------------------------------------------------------------------
class func viewController() -> ReportC {
return StoryBoard.main.instantiateViewController(withIdentifier: StoryBoard.controller.ReportC) as! ReportC
}
//--------------------------------------------------------------------------------
创建标识符的常量
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 :(得分:0)
我使用的方法更好:
第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 {
static func instantiate(fromAppStoryboard appStoryboard: AppStoryboard) -> Self {
return appStoryboard.viewController(viewControllerClass: self)
}
}
第2步: 在故事板中分配与视图控制器的类名相同的故事板ID。
第3步: 现在创建viewcontroller的对象,如:
let notificationVC = NotificationVC.instantiate(fromAppStoryboard: .Notification)
/* Syntax:
let <yourObject> = <YourViewController's class name>.instantiate(fromAppStoryboard: <respected storyboard>) */
notificationVC
将成为NotificationVC
现在玩你的对象。 :)