0

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

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

1 回答 1

0

if you want to retain memory UINavigationController . I suggest you hold it in NSMutableArray.

In header file

@property(nonatomic, strong) NSMutableArray* arrayNavigationCont;

In implementation file

@synthesize arrayNavigationCont = _arrayNavigationCont;

do not forget also to init array in somewhere relevant like viewDidLoad

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

In your method ...

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

}

Later you can access you navigationConts somewhere again

for(int i = 0; i < [self.arrayNavigationCont count]; i++)
{
    UINavigationController *nav1 = [self.arrayNavigationCont objectAtIndex:i];
    //do sth with nav1 to your like
}
于 2013-04-15T14:31:25.150 回答