2016-03-10 44 views
0

我有使用API​​收集不同電視頻道的時間表列表的程序,我可能有10個電視頻道,所以我不想手動添加我的tabBar控制器中的10個項目在Objective C中添加tabBarController項目

我用這個代碼,以收集我的渠道:

- (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]; 
    } 
} 

我想知道我如何與可以爲每個通道添加一個水龍頭欄項目

謝謝你們

+0

不能在tabbar控制器中添加超過五個選項卡項目。另一種選擇是創建自定義的可滾動標籤欄,您可以根據需要添加標籤頁。 –

+0

當超過5個項目時,它們會自動放入「更多」項目中。 –

+0

是的,它們會自動添加到tableview格式的更多選項卡中。 –

回答

0

此代碼適用於我...您可以複製並粘貼它... :)

- (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; 
}