Xcode UITabBarController:使两个选项卡指向同一个 ViewController [英] Xcode UITabBarController: Make Two tabs point to same ViewController

查看:26
本文介绍了Xcode UITabBarController:使两个选项卡指向同一个 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个简单的应用程序来查看烂番茄的电影列表(作为学习 ios 开发的一部分)https://raw.githubusercontent.com/koldkoder/movie_listing/master/rotten_tomatoes.gif

I built a simple app to view movie listing from rotten tomatoes(as a part of learning ios development)https://raw.githubusercontent.com/koldkoder/movie_listing/master/rotten_tomatoes.gif

我正在尝试添加选项卡控件.第一个选项卡将列出当前票房电影,第二个选项卡列出最近在 DVD 上发行的电影.两个视图完全相同,只是它们从不同的 api 端点获取数据.我想使用 UITabBarController 来实现这个功能.但我不想创建两个重复的 Viewcontrollers,而是为了这两个目的而使用一个.使用情节提要和代码执行此操作的正确方法是什么.

I am trying to add tab control. One tab would list current box office movies, and second tab to list movies out on DVD recently. Both Views are exactly same, just they get data from different api endpoint. I want to use UITabBarController to implement this functionality. But i dont want to create two duplicate Viewcontrollers, instead use one for both the purpose. What is the right way of doing this, using storyboard, and code.

推荐答案

这很简单.创建一个 UIViewController,它在初始化程序中接受一个视图类型参数,如下所示:

This is pretty straight forward. Create one UIViewController that takes a view type param in initializer like this:

typedef NS_ENUM (NSInteger, MediaViewType) {
    MediaViewTypeBoxOffice = 0,
    MediaViewTypeDVD
};

- (id)initWithViewType:(MediaViewType)iViewType;

在实现文件中,处理您的视图 &基于传递的视图类型的功能.然后将您的视图控制器的实例添加到 UITabBarController:

In the implementation file, handle your view & functionality based on passed view type. Then add your view controller's instances to UITabBarController:

MyMediaViewController *vc1 = [MyMediaViewController alloc] initWithViewType: MediaViewTypeBoxOffice];
MyMediaViewController *vc2 = [MyMediaViewController alloc] initWithViewType: MediaViewTypeDVD];

self.tabBarController.viewControllers = @[vc1, vc2];

这篇关于Xcode UITabBarController:使两个选项卡指向同一个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆