如何在iOS中根据不同的用户类型动态切换UI主题

如何在iOS中根据不同的用户类型动态切换UI主题,ios,swift,design-patterns,swift4.2,xcode10.1,Ios,Swift,Design Patterns,Swift4.2,Xcode10.1,在我的应用程序中,我有一个场景,需要根据用户类型切换UI主题设计。例如:在我的Type1用户流中,它类似于注册屏幕->主页屏幕,而在我的Type2用户流中,它应该类似于注册屏幕->联系人屏幕->主页屏幕。对于类型2用户,UI设计和主题也不同。为了实现这一点,下面是我目前实现的示例代码流 注册视图控制器 (此视图可供两个用户使用,但UI主题不同,如导航栏颜色、背景颜色、按钮颜色、字体、图像等) ContactViewController (此视图仅适用于type1用户) HomeViewContr

在我的应用程序中,我有一个场景,需要根据用户类型切换UI主题设计。例如:在我的Type1用户流中,它类似于
注册屏幕->主页屏幕
,而在我的Type2用户流中,它应该类似于
注册屏幕->联系人屏幕->主页屏幕
。对于类型2用户,UI设计和主题也不同。为了实现这一点,下面是我目前实现的示例代码流

注册视图控制器

(此视图可供两个用户使用,但UI主题不同,如导航栏颜色、背景颜色、按钮颜色、字体、图像等)

ContactViewController (此视图仅适用于type1用户)

HomeViewController (此视图可供两个用户使用,但UI主题不同,如注册中所述)

这很好,但这里的问题是,现在我在实用程序中定义了一个
isUserType
,它的可扩展性并不完美。对于每个流和UI更改,我需要基于此参数设置if-else条件。所以,现在如果我有另一个用户类型需要在将来添加,我将再次需要另一个if-else语句,并在此基础上切换UI和流


有没有更好的方法来解决这个问题?

您可以检查主题的更改

您需要根据不同的用户更改动态主题,以便发布通知并根据需要应用主题

// We create a model
struct Theme {
    let theme: String
    let fontColor: UIColor
    let alpha: CGFloat
}

// We need a protocol for we don't want all view controller listen theme
protocol Themeable: class {
    func listenTheme()
    func didThemeChange(theme: Theme)
}


// Global notification name
let themeableNotificationName = Notification.Name(rawValue: "ThemeableNotification")

// Our protocol extension and observer notification
extension Themeable where Self: UIViewController {
    func listenTheme() {
        NotificationCenter.default.addObserver(forName: themeableNotificationName, object: nil, queue: nil) { [weak self] notification in
            guard let theme = notification.object as? Theme else { return }
            self?.didThemeChange(theme: theme)
        }
    }

}


// Notification sender themeController
class NotifyThemeController: UIViewController {


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // Create a model and post
        NotificationCenter.default.post(name: themeableNotificationName, object: Theme(theme: "Lorem", fontColor: .red, alpha: 1.0), userInfo: nil)
    }

}

// YViewController
class YViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // We need call this method for observer
        listenTheme()
    }


}
// YViewController conforms Themeable
extension YViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}
// ZViewController
class ZViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // We need call this method for observer
        listenTheme()
    }


}
// ZViewController conforms Themeable
extension ZViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}

玩得开心

主题定义应基于用户类型/类别
override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }


private func setupViews(){

        //Setupviews 

}

 @IBAction func continueAction(_ sender: Any) {

            gotToHomeView()

}
override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }



private func setupViews(){

        if Utilities.isUserType1{
            setupViewsForType1User()
        } else {
            setupViewsForType2User()
    }

}
// We create a model
struct Theme {
    let theme: String
    let fontColor: UIColor
    let alpha: CGFloat
}

// We need a protocol for we don't want all view controller listen theme
protocol Themeable: class {
    func listenTheme()
    func didThemeChange(theme: Theme)
}


// Global notification name
let themeableNotificationName = Notification.Name(rawValue: "ThemeableNotification")

// Our protocol extension and observer notification
extension Themeable where Self: UIViewController {
    func listenTheme() {
        NotificationCenter.default.addObserver(forName: themeableNotificationName, object: nil, queue: nil) { [weak self] notification in
            guard let theme = notification.object as? Theme else { return }
            self?.didThemeChange(theme: theme)
        }
    }

}


// Notification sender themeController
class NotifyThemeController: UIViewController {


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // Create a model and post
        NotificationCenter.default.post(name: themeableNotificationName, object: Theme(theme: "Lorem", fontColor: .red, alpha: 1.0), userInfo: nil)
    }

}

// YViewController
class YViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // We need call this method for observer
        listenTheme()
    }


}
// YViewController conforms Themeable
extension YViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}
// ZViewController
class ZViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // We need call this method for observer
        listenTheme()
    }


}
// ZViewController conforms Themeable
extension ZViewController: Themeable {
    func didThemeChange(theme: Theme) {
        // TODO UI
    }

}