基于按钮推入式设计分支出单个视图控制器

时间:2019-07-07 03:14:43

标签: swift segue

基本上,我正在尝试根据所按的按钮从单个视图控制器中分支出不同的控制器。

我在初始视图控制器中有2个按钮。按钮'A'创建类A的对象,按钮'B'创建类B的对象。A和B都是类C的子类。从初始视图控制器到下一个视图,我将对象类型设置为C。如果需要访问a或b的特定属性,应如何设置下一个视图控制器变量类型以接受A或B。

    // A is parent class, B, C and D inherit and have more attributes
    var account: A = A(firstName: "", lastName: "", userName: "", email: "")

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

    @IBAction func accountTypeTapped(_ sender: RoundButton) {
        switch sender.tag {
        case 1:
            account = B(firstName: "", lastName: "", userName: "", email: "", companyName: "")
        case 2:
            account = A(firstName: "", lastName: "", userName: "", email: "")
        case 3:
            account = C(firstName: "", lastName: "", userName: "", email: "", companyName: "")
        case 4:
            account = D(firstName: "", lastName: "", userName: "", email: "", currentLocation: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0))
        default:
            break
        }
    }




override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toRegisterTwo" {
            print(segue.destination)
            let registerTwo = segue.destination as! RegisterTwoController


// What type should registerTwo.account be? If I set it to class A I cant access class B specific attributes, same for other classes. 
            registerTwo.account = account
            print(account)
        }
    }





==========================================================


// In a different controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toAddEmployeesAndConsultants" {
            let recievingVC = segue.destination as! AddEmployeesAndTConsultantsController
            recievingVC.account = account
        }

        if segue.identifier == "toAddEmployees" {
            let recievingVC = segue.destination as! AddEmployeesViewController
            recievingVC.account = D(firstName: account.firstName, lastName: account.lastName!, userName: account.userName, email: account.email, companyName: account.companyName)
        }
    }

这是个好方法吗?有没有更好的办法?谢谢!

0 个答案:

没有答案