How to subclass UITableViewController in Swift

后端 未结 9 1858
栀梦
栀梦 2020-12-23 11:42

I want to subclass UITableViewController and be able to instantiate it by calling a default initializer with no arguments.

class TestViewController: UITableV         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 12:10

    I wanted to subclass UITableViewController and add a non-optional property which requires overriding the initializer and dealing with all the problems described above.

    Using a Storyboard and a segue gives you more options if you can work with an optional var rather than a non-optional let in your subclass of UITableViewController

    By calling performSegueWithIdentifier and overriding prepareForSegue in your presenting view controller, you can get the instance of the UITableViewController subclass and set the optional variables before initialization is completed:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "segueA"{
                var viewController : ATableViewController = segue.destinationViewController as ATableViewController
                viewController.someVariable = SomeInitializer()
            }
    
            if segue.identifier == "segueB"{
                var viewController : BTableViewController = segue.destinationViewController as BTableViewController
                viewController.someVariable = SomeInitializer()
            }
    
        }
    

提交回复
热议问题