How do I subclass a UITabbarcontroller using storyboards?

左心房为你撑大大i 提交于 2019-12-13 18:17:10

问题


I have a tabbar application using storyboards. Tab 1 is a UIViewController with Mapview & Tab 2 is a UITableViewController.

The appdelegate calls for a web fetch (via a custom class) and takes that web response and pareses it (via a custom class) and then puts the info in a CD-db (via a custom class).

MapVC fetches results from the CD-db and populates an NSMutableArray (property) to be looped thru. The array contains a custom CD-db object with 2 coordinates which are used to create an MKAnnotation in the for loop. The distance from each MKAnnotation to userLocation is calculated and the MyLocation Class object is completed with a distance value in its subtitle property. Thus each pin on the map displays a title (name) & subtitle (distance).

TableVC creates a separate fetch to the CD-db and populates its array property to be used in the cellForRowAtIndexPath (CFRAIP).

Now I want the tableview to also display the distance in each cell. So it has been suggested I subclass the uitabbarcontroller and set the delegate/datasource of the TableVC.tableview to the MapVC. My question is, how do I subclass my uitabbarcontroller and set the delegate and datasource?

Something like: in app delegate get the window's rootviewcontroller?


回答1:


First you need to subclass UITabBarController. Make a new file in Xcode, set UITabBarController as the parent class.

Then go to your storyboard and select the tabbarcontroller, set its custom class to the class you just make.

In viewDidLoad of the tabVC get its sub VC's and find the ones you want. Here's the pseudo code, I can tidy it up later:

CSMapListViewController *mapListViewController;
CSTableViewController *tableViewController;

for (UIViewController *vc in self.viewControllers)
{
    if ([vc isKindOfClass:[CSMapListViewController class]])
    {
        mapListViewController = (CSMapListViewController *)vc;
    } else if ([vc isKindOfClass:[CSTableViewController class]])
    {
        tableViewController = (CSTableViewController *)vc;
    } 
}

tableViewController.tableview.datasource = mapListViewController;

Hope that helps.



来源:https://stackoverflow.com/questions/14490689/how-do-i-subclass-a-uitabbarcontroller-using-storyboards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!