简体   繁体   中英

How to make UITableView inherit custom subclass of UIViewController?

I'm new at iOS development. I have two table views and a content detail view something like this:

PopularContentViewController : UITableViewController
LatestContentViewController : UITableViewController
ContentDetailViewController : UIViewController

I want to add some authentication so that before any of these controllers is loaded, I can ensure that the user is logged in, and if not, show a modal login view controller.

My thinking was to create my own UIViewController subclass (say, AuthenticatedViewController) and make my other controllers inherit it. The problem is, how do I do something like this:

PopularContentViewController : UITableViewController : AuthenticatedViewController
LatestContentViewController : UITableViewController : AuthenticatedViewController
ContentDetailViewController : AuthenticatedViewController

I found this similar question: Can I create a UITableViewController that inherits from a custom subclass of UIViewController?

... but I'm wondering if there's another way to do this other than inheritance or creating my own UITableViewController that extends my AuthenticatedTableViewController? Maybe through protocols or by using a delegate instead? Just looking for a nudge in the right direction.

In objective-c you can only inherit from one superclass. There is no way to inherit multiple classes. You can try another approach like you can keep your AuthenticatedViewController in appDelegate and you can access that from any class because appDelegate is a singleton class. So this way you can achieve the functionality you want.

Uitableviewcontrollers is a UIViewController. But you're going about this the wrong way. Create a root view controller for authentication, perhaps place that in a navigation controller. Then one the user is authenticated, push one of your tableview controllers onto the stack (or replace the original controller entirely at runtime and save off it's credentials). ...

Or, if you insist, use categories or a singleton just to deal with authentication back end stuff (leave the UI out of that when possible). I'd go the way of one view controller per job (in a navigation controller container)... Simplify, man, simplify.

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