简体   繁体   中英

Can I have UITableViewController and UIViewController inherit from the same class?

I just started creating apps for iOS and am still trying to get a hold of some fundamental properties.

I have a class that I call GetData which gathers data from my web server.

I'm using both an ordinary ViewController and TableViewController in my app but if I specify GetData like this:

@interface GetData: UITableViewController<...> 

I can't use GetData for my ViewController and vice versa.

How can I fix this problem?

You can't and shouldn't subclass it the way you showed. Getting data from a remote server doesn't belong to the controller. You should move it a separate class. I think that this article might help you clear your thinking.

Use GetData as a subclass of UIViewController, add a table view in it, and use GetData as the TableViewDatasource and TableViewDelegate

But as pointed by Holko, you should not use same class for fetching data from server, divide your code into model, view and controller, write a custom class that performs the task for getting the data from server. and use that class in your view controller.

If you use the same class for doing network tasks and managing UI it might lead to unresponsive UI which is bad UX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM