在ios中动态创建导航栏

时间:2013-04-15 13:08:58

标签: iphone ios objective-c

这是我在项目中设置的静态导航栏,但是想以动态的方式做,这里是两种类型的代码......

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;

现在我从URL获取数据并希望将其指定为动态导航项。但我现在知道了,不知道怎么做。

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

1 个答案:

答案 0 :(得分:0)

如果你想保留记忆UINavigationController。我建议你在NSMutableArray中保留它。

在头文件

@property(nonatomic, strong) NSMutableArray* arrayNavigationCont;

在实施档案

@synthesize arrayNavigationCont = _arrayNavigationCont;

不要忘记在viewDidLoad

之类的相关位置使用init数组
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
}