【问题标题】:Two UIViews that share ALMOST the same UIViewController两个 UIView 共享几乎相同的 UIViewController
【发布时间】:2023-03-23 08:16:01
【问题描述】:

我有一个产品的“购买”页面和一个“销售”页面。它们共享相同的 UIViewController,除了在一个 UIViewController 中显示待售物品和另一个用于购买的事实。 (另一个区别是一个按钮 - 它在一个按钮中显示字符串 A,在另一个按钮中显示 B。

我可以让它们与这些更改共享相同的 UIViewController 吗?我可以为每个视图定义一些参数吗?

【问题讨论】:

  • 你可以使用一个基类UIViewController,它有两个非常不同的子类。将所有逻辑放在基类中,仅将差异放在子类中......
  • 您能否尝试分享一些代码示例以更好地解释您的想法?
  • 我相信最好的做法是避免子类化,但是我在 ViewControllers 的情况下例外,因为它们的层次结构已经被大量子类化了。会做@Grimxn 建议的事情

标签: swift xcode uiview uiviewcontroller


【解决方案1】:

你可以使用一个基础的 UIViewController,它有两个非常不同的子类。将所有逻辑放在基类中,只将差异放在子类中......

class BaseVC: UIViewController {
    @IBOutlet weak var button: UIButton!
    var items: [String] = []
    @IBAction func buttonPressed(_ sender: Any) {
        // do stuff
    }
    // add all of your tableView or similar common stuff here...
}
class AVC: BaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.button.titleLabel?.text = "A"
        self.items = ["Sell1", "Sell2"]
    }
}
class BVC: BaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.button.titleLabel?.text = "B"
        self.items = ["Buy1", "Buy2"]
    }
}

【讨论】:

  • 我的视图中有一个 UICollectionView。这是否意味着我必须从两个不同的视图将插座连接到它?它不会让我的应用崩溃吗?
【解决方案2】:

创建一个xib文件, 在其中添加一个视图, 在视图控制器中使用该视图,在不同按钮上具有不同的功能

观看此视频寻求帮助:https://www.youtube.com/watch?v=tvQxXoV527w&t=754s

【讨论】:

    【解决方案3】:

    使用 BaseViewController 并使其成为新视图控制器的父类

    如下所示:

    基础控制器: 导入 UIKit

    class BaseViewController: UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        //Common initiation codes
    }
    
    // Write all the codes that can be shared between the controllers
    }
    

    为每个视图控制器创建单独的类并根据需要修改它们:

    class SettingsController: BaseViewController {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()//This calls everything you wrote as init code for BaseController
    
        //Write any view controller specific init code here
    }
    
    //If you have any other settings controller specific code, write here
    }
    

    由于我没有看到你的代码,我不能确定这是否是正确的方法。这种方法仍然意味着您不必为每个视图控制器类重写代码并且仍然保持干净。 当您通过代码进行布局和视图时,首选上述方法。

    如果您使用故事板并且根据您的具体需要,我建议您改为这样做:

    //Initiate your controller like this from the previous controller
    @IBAction func goToSettingsController(_ sender: Any) {
        let baseController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ControllerID") as! BaseController
        baseController.id = "Settings"
        self.present(baseController, animated: true, completion: nil)
    
    }
    
    @IBAction func goToAboutController(_ sender: Any) {
        let baseController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ControllerID") as! BaseController
        baseController.id = "About"
        self.present(baseController, animated: true, completion: nil)
    }
    

    在你的 BaseController 中: 类 BaseController: UIViewController{

    var id: String!
    @IBOutlet weak var backGround: UIView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        customInit()
    }
    
    func customInit(){
        switch id{
        case "Settings":
            self.backGround.backgroundColor = .purple //Any controller specific code
            break
        case "About":
            self.backGround.backgroundColor = .green //Any controller specific code
            break
        default:
            break
        }
    
    }
    

    }

    您不需要前面提到的三个单独的类,但您可以为两个 ViewController 使用 BaseController 类。

    【讨论】:

    • 我的视图中有一个 UICollectionView。这是否意味着我必须从两个不同的视图将插座连接到它?它不会让我的应用崩溃吗?
    • 我还会说明另一种方法。
    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多