2013-04-15 104 views
0

這是靜態的導航欄我在我的項目是如何設置的,但要在動態的方式做的,這裏是兩個類型的代碼...創建導航欄dynamatically在IOS

viewsArray = [[NSArray alloc] init]; 
    AfterloginViewController *toolsnavigation = [[AfterloginViewController alloc] init]; 
    toolsnavigation.tabBarItem.image = [UIImage imageNamed:@"cool.png"]; 
    [toolsnavigation setTitle:@"Tools"]; 
    UINavigationController *nav0 = [[UINavigationController alloc] initWithRootViewController:toolsnavigation]; 

MapViewController *myridenavigation = [[MapViewController alloc] init]; 
    myridenavigation.tabBarItem.image = [UIImage imageNamed:@"cool.png"]; 
    [myridenavigation setTitle:@"Login"]; 
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation]; 

viewsArray = [NSArray arrayWithObjects:nav0,nav1,nav2,nav3,nav4,nav5,nav6,nav7,nav8, nil]; 

    tabbarController = [[UITabBarController alloc] init]; 
    [tabbarController setViewControllers:viewsArray]; 
    self.window.rootViewController = tabbarController; 

現在我得到來自網址的數據並希望將其分配爲動態導航項目。但我現在已經開始拼命了,不知道該怎麼做。

NSString *loginstring = [NSString stringWithFormat:@"%@nvgationarray.php",mydomainurl]; 
    NSMutableData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: loginstring]]; 
    NSDictionary *allData = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil]; 

    int i = 0; 
    for(NSDictionary *stat in allData) 
    { 
     NSString *ssprst = [stat objectForKey:@"tab_type"]; 
     NSString *ssprst1 = [stat objectForKey:@"tab_name"]; 
     NSString *ssprst2 = [stat objectForKey:@"tab_id"]; 
     NSString *ssprst3 = [stat objectForKey:@"icon"]; 
     NSLog(@"all data ===== :::: %@ %@ %@ %@",ssprst,ssprst1,ssprst2,ssprst3); 
     NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++"); 

     AbcViewController *myridenavigation = [[AbcViewController alloc] init]; 
     myridenavigation.tabBarItem.image = [UIImage imageNamed:@"cool.png"]; 
     [myridenavigation setTitle:@"Login"]; 
     UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation]; 
     i++; 
    } 
+0

你要分配數據同步(等待數據接收,然後創建導航欄)?或異步(無需等待數據接收,做什麼,當數據接收創建導航欄) –

+0

數據正在接收,現在我必須分配到導航控制器.. – Shyantanu

+0

所以它是同步?你想創建大量的uiviewcontroller或uinavigationcontroller嗎?我創建許多uinavigationcontroller似乎很奇怪。你想保留uinavigationcontroller的記憶嗎? –

回答

0

如果你想保留內存UINavigationController。我建議你把它放在NSMutableArray

在頭文件

@property(nonatomic, strong) NSMutableArray* arrayNavigationCont; 

在實現文件

@synthesize arrayNavigationCont = _arrayNavigationCont; 

不也忘了初始化數組中的有關地方,比如viewDidLoad

self.arrayNavigationCont = [[NSMutableArray alloc] init]; 

在你的方法 ...

for(NSDictionary *stat in allData) 
{ 
     ... 
     AbcViewController *myridenavigation = [[AbcViewController alloc] init]; 
     myridenavigation.tabBarItem.image = [UIImage imageNamed:@"cool.png"]; 
     [myridenavigation setTitle:@"Login"]; 
     UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:myridenavigation]; 
     [self.arrayNavigationCont addObject:nav1] 
     i++; 

} 

稍後,您可以再次訪問的地方,你navigationConts

for(int i = 0; i < [self.arrayNavigationCont count]; i++) 
{ 
    UINavigationController *nav1 = [self.arrayNavigationCont objectAtIndex:i]; 
    //do sth with nav1 to your like 
}