Ios 在目标C中添加tabBarController项

Ios 在目标C中添加tabBarController项,ios,objective-c,Ios,Objective C,我有一个使用API收集不同电视频道时间表列表的程序,我可能有10个电视频道,所以我不想在我的tabBar控制器中手动添加这10个项目 我使用此代码收集我的频道: - (void)viewDidLoad { [super viewDidLoad]; NSString *url = [NSString stringWithFormat: @"https://apis.is/tv"]; // Download JSON NSData *JSONData = [NSDat

我有一个使用API收集不同电视频道时间表列表的程序,我可能有10个电视频道,所以我不想在我的tabBar控制器中手动添加这10个项目

我使用此代码收集我的频道:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *url = [NSString stringWithFormat: @"https://apis.is/tv"];
    // Download JSON
    NSData *JSONData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    // Parse JSON
    NSError* error;
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:JSONData //1

                          options:kNilOptions
                          error:&error];
    NSArray* jsonResult = [json objectForKey:@"channels"];
    _channelList = [[NSMutableArray alloc] init];

    for (id item in jsonResult) {
        TVShow *TVChannel = [[TVShow alloc] init];
        TVChannel.channel = [item objectForKey:@"name"];
        TVChannel.endpoint = [item objectForKey:@"endpoint"];
        [_channelList addObject:TVChannel];
    }
}
我想知道如何为每个频道添加一个点击栏项目


谢谢大家

这段代码对我来说很好用。。。您可以复制并粘贴它…:)


无法在tabbar控制器中添加五个以上的选项卡项。另一个选项是创建自定义的可滚动选项卡栏,您可以在其中添加任意数量的选项卡。当项目超过5个时,它们会自动放入“更多”项目中否?是,它们将以tableview格式自动添加到“更多”选项卡中。因此,您知道我如何通过代码而不是故事板来放入项目吗?很抱歉,我无法获取您的信息。你能解释一下吗?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *url = [NSString stringWithFormat: @"https://apis.is/tv"];
// Download JSON
NSData *JSONData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
// Parse JSON
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:JSONData //1

                      options:NSJSONReadingAllowFragments
                      error:&error];
NSArray* jsonResult = json[@"results"][0][@"channels"];
_channelList = [[NSMutableArray alloc] init];

for (id item in jsonResult) {
    TVShow *TVChannel = [[TVShow alloc] init];
    TVChannel.channel = item[@"name"];
    TVChannel.endpoint = item[@"endpoint"];
    [_channelList addObject:TVChannel];
}


NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
NSInteger tabTag = 0;
for (TVShow *show in self.channelList) {
    ViewController1 *view1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:[NSBundle mainBundle]];
    [tabViewControllers addObject:view1];
    view1.mainLabel.text = show.channel;
    view1.tabBarItem = [[UITabBarItem alloc] initWithTitle:show.channel
                                                     image:nil
                                                       tag:tabTag];
    tabTag++;
}
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:tabViewControllers];
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
return YES;
}