【发布时间】:2019-07-24 08:23:32
【问题描述】:
在我的应用程序中,我有一个场景需要根据用户类型切换 UI 主题设计。例如:在我的 Type1 用户流中,它类似于 Registration Screen -> HomePage Screen,而在我的 Type 2 用户中,它应该类似于 Registration Screen-> Contact Screen -> Home Page Screen。并且对于 Type 2 用户来说,UI 设计和主题是不同的。为了实现这一点,下面是我当前实现的示例代码流。
RegistrationViewController
(此视图可供两个用户使用,但 UI 主题不同,如导航栏颜色、背景颜色、按钮颜色、字体、图像等)
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func setupViews(){
if Utilities.isUserType1{
setupViewsForType1User() //Adds themes for type 1 user
} else {
setupViewsForType2User() //Adds themes for type 2 user
}
}
@IBAction func continueAction(_ sender: Any) {
if Utilities.isUserType1{
goToContactView() //Goes to ContactViewController
} else {
gotToHomeView() //Goes to HomeViewController
}
}
ContactViewController (此视图仅适用于 type1 用户)
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func setupViews(){
//Setupviews
}
@IBAction func continueAction(_ sender: Any) {
gotToHomeView()
}
HomeViewController (此视图可供两个用户使用,但 UI 主题与注册中提到的不同)
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func setupViews(){
if Utilities.isUserType1{
setupViewsForType1User()
} else {
setupViewsForType2User()
}
}
这工作正常,但这里的问题是现在我在实用程序中定义了一个isUserType,并且它不是完全可扩展的。对于每个流程和 UI 更改,我需要根据此参数设置一个 if-else 条件。因此,现在如果我有另一个需要在未来添加的用户类型,我将再次需要另一个 if-else 语句并基于此切换 UI 和流程。
有没有更好的方法来解决这个问题?
【问题讨论】:
-
你的主题定义应该基于 userType/category
标签: ios swift design-patterns swift4.2 xcode10.1