使用tabbar ios

时间:2015-07-06 12:04:37

标签: ios memory-management uitabbarcontroller

我在用户登录应用时使用tabbar,因此我提供了tabbar。当用户logout输出并再次登录时,会创建相同的tabbar,但会增加内存。 如果有人有解决方案,请告诉我

谢谢。 这里我正在创建tabbar

-(void)CreateTabBar:(NSString *)userType
{

[self createViewsForSupplementalTab];
[self.tabBarController.tabBar setBarTintColor:[UIColor blackColor]];
[self.tabBarController.tabBar setTranslucent:NO];
[self.tabBarController.tabBar setBackgroundColor:[UIColor colorWithRed:30.0/255.0 green:30.0/255.0 blue:30.0/255.0 alpha:1]];
[self.tabBarController.tabBar setTintColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1]];
[self.tabBarController setDelegate:self];

if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {

    self.tabBarController.view.frame = CGRectMake(0, 0, 320, 568);

} else {

    self.tabBarController.view.frame = CGRectMake(0, 0, 768, 1024);
    //  self.tabBarController.tabBar.frame=CGRectMake(self.tabBarController.tabBar.frame.origin.x, self.tabBarController.tabBar.frame.origin.y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height+50);

    CGRect viewFrame = self.tabBarController.tabBar.frame;
    viewFrame.size.height = 520;
    self.tabBarController.tabBar.frame = viewFrame;
}

if ([userType isEqualToString:@"Player"]) {

    [firstNavController setViewControllers:[NSArray arrayWithObject:self.menuVC] animated:YES];
    [secondNavController setViewControllers:[NSArray arrayWithObject:self.rangeFinder] animated:YES];
    [ThirdNavController setViewControllers:[NSArray arrayWithObject:self.weatherVC] animated:YES];
    [fourthNavController setViewControllers:[NSArray arrayWithObject:self.messageVC] animated:YES];
    [fifthNavController setViewControllers:[NSArray arrayWithObject:self.golferVC] animated:YES];

    if (tabs.count!=0) {
        [tabs removeAllObjects];
    }

    [tabs addObject:firstNavController];
    [tabs addObject:secondNavController];
    [tabs addObject:ThirdNavController];
    [tabs addObject:fourthNavController];
    [tabs addObject:fifthNavController];
    [self.tabBarController setViewControllers:tabs];

    UIImage *activeImage = [UIImage imageNamed:@"Golfier_Suggest.png"];
    activeImage = [activeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIImage *inactiveImage =[UIImage imageNamed:@"Golfier_Sel_Suggest.png"];
    inactiveImage =[inactiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.menuVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Menu" image:activeImage selectedImage:inactiveImage];

    activeImage = [UIImage imageNamed:@"Flag_Suggest.png"];
    activeImage = [activeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    inactiveImage =[UIImage imageNamed:@"Flag_Sel_Suggest.png"];
    inactiveImage =[inactiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // self.playVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"GPS" image:activeImage selectedImage:inactiveImage];
    //self.golferRFVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"GPS" image:activeImage selectedImage:inactiveImage];
    self.rangeFinder.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"GPS" image:activeImage selectedImage:inactiveImage];

    activeImage = [UIImage imageNamed:@"weather.png"];
    activeImage = [activeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    inactiveImage =[UIImage imageNamed:@"weatherSEL.png"];
    inactiveImage =[inactiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.weatherVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Weather" image:activeImage selectedImage:inactiveImage];

    activeImage = [UIImage imageNamed:@"Mail_suggest.png"];
    activeImage = [activeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    inactiveImage =[UIImage imageNamed:@"Mail_Sel_suggest.png"];
    inactiveImage =[inactiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.messageVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Message" image:activeImage selectedImage:inactiveImage];

    activeImage = [UIImage imageNamed:@"user_suggest.png"];
    activeImage = [activeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    inactiveImage =[UIImage imageNamed:@"user_Sel_suggest.png"];
    inactiveImage =[inactiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.golferVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"MyGolfer" image:activeImage selectedImage:inactiveImage];
    self.tabBarController.selectedIndex = 1;
    return;
}


if ([userType isEqualToString:@"CR"]) {
    //firstNavController = [[UINavigationController alloc] initWithRootViewController:ranger];

    [firstNavController setViewControllers:[NSArray arrayWithObject:ranger] animated:YES];

}
else
{
    [firstNavController setViewControllers:[NSArray arrayWithObject:kitchenView] animated:YES];

    // firstNavController = [[UINavigationController alloc] initWithRootViewController:kitchenView];
}
[secondNavController setViewControllers:[NSArray arrayWithObject:orderView] animated:YES];
[ThirdNavController setViewControllers:[NSArray arrayWithObject:analyticView] animated:YES];
[fourthNavController setViewControllers:[NSArray arrayWithObject:messageView] animated:YES];
[fifthNavController setViewControllers:[NSArray arrayWithObject:adimnView] animated:YES];

if (tabs !=0) {
    [tabs removeAllObjects];
}

[tabs addObject:firstNavController];
[tabs addObject:secondNavController];
[tabs addObject:ThirdNavController];
[tabs addObject:fourthNavController];
[tabs addObject:fifthNavController];
[self.tabBarController setViewControllers:tabs];

// setting the active and selected image for the active View

UIImage *ActiveImageRanger = [UIImage imageNamed:@"icon1.png"];
ActiveImageRanger = [ActiveImageRanger imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
ranger.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Gopher View" image:ActiveImageRanger selectedImage:ActiveImageRanger];


UIImage *ActiveImage = [UIImage imageNamed:@"icon1.png"];
ActiveImage = [ActiveImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
kitchenView.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Active Orders" image:ActiveImage selectedImage:ActiveImage];


UIImage *orderImage;
if ([userType isEqualToString:@"CR"]) {
    orderImage = [UIImage imageNamed:@"icon2-hv.png"];
}
else
{
    orderImage = [UIImage imageNamed:@"icon2.png"];
}



// setting the active and selected image for the active View
//UIImage *orderImage = [UIImage imageNamed:@"icon2.png"];
orderImage = [orderImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
orderView.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Order History" image:orderImage selectedImage:orderImage];



UIImage *AnalyticsImage;
if ([userType isEqualToString:@"CK"]||[userType isEqualToString:@"CC"]||[userType isEqualToString:@"CR"]) {
    AnalyticsImage = [UIImage imageNamed:@"icon3-hv.png"];
}
else
{
    AnalyticsImage = [UIImage imageNamed:@"icon3.png"];
}

// setting the active and selected image for the active View

AnalyticsImage = [AnalyticsImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
analyticView.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Analytics" image:AnalyticsImage selectedImage:AnalyticsImage];

// setting the active and selected image for the active View
UIImage *MessageImage = [UIImage imageNamed:@"icon4.png"];
MessageImage = [MessageImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
messageView.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Messages" image:MessageImage selectedImage:MessageImage];

UIImage *adminImage;
if ([userType isEqualToString:@"CK"]||[userType isEqualToString:@"CC"]||[userType isEqualToString:@"CR"]) {
    adminImage = [UIImage imageNamed:@"icon5-hv.png"];
}
else
{
    adminImage = [UIImage imageNamed:@"icon5.png"];
}

// setting the active and selected image for the active View
adminImage = [adminImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
adimnView.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Admin Tools" image:adminImage selectedImage:adminImage];

self.tabBarController.selectedIndex = 0;
if ([userType isEqualToString:@"CK"]||[userType isEqualToString:@"CC"]) {
    [[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:FALSE];
    [[[[self.tabBarController tabBar]items]objectAtIndex:4]setEnabled:FALSE];

}
else if([userType isEqualToString:@"CR"])
{
    [[[[self.tabBarController tabBar]items]objectAtIndex:1]setEnabled:FALSE];
    [[[[self.tabBarController tabBar]items]objectAtIndex:2]setEnabled:FALSE];
    [[[[self.tabBarController tabBar]items]objectAtIndex:4]setEnabled:FALSE];
}
//  }

}

这里我正在为tabbar创建视图

- (void)createViewsForSupplementalTab {
//check for user and implement the views
if ([[SingletonClass singleton].userType isEqualToString:@"Player"] ) {

    if (self.menuVC==nil) {
        self.menuVC=[[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    }
    if (self.playVC==nil) {
        self.playVC=[[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil];
    }
    if (self.rangeFinder==nil) {
        self.rangeFinder=[[rangeFinderContoller alloc] initWithNibName:@"rangeFinderContoller" bundle:nil];
    }
    if (self.weatherVC==nil) {
        self.weatherVC=[[WeatherViewController alloc] initWithNibName:@"WeatherViewController" bundle:nil];
    }
    if (self.messageVC==nil) {
        self.messageVC=[[GMessageViewController alloc] initWithNibName:@"GMessageViewController" bundle:nil];
    }
    if (self.golferVC==nil) {
        self.golferVC=[[myGolferViewController alloc] initWithNibName:@"myGolferViewController" bundle:nil];
    }




} else
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        if (kitchenView==nil) {
            kitchenView=[[KitchenViewController alloc] initWithNibName:@"KitchenViewController" bundle:nil];
        }
        if (orderView==nil) {
            orderView=[[OrderHistoryController alloc] initWithNibName:@"OrderHistoryController" bundle:nil];
        }
        if (analyticView==nil) {
            analyticView=[[AnalyticsViewController alloc] initWithNibName:@"AnalyticsViewController" bundle:nil];
        }
        if (messageView==nil) {
            messageView=[[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
        }
        if (adimnView==nil) {
            adimnView=[[AdminViewController alloc] initWithNibName:@"AdminViewController" bundle:nil];
        }
        if (ranger==nil) {
            ranger=[[RangerViewController alloc] initWithNibName:@"RangerViewController" bundle:nil];
        }


    } else {

        if (kitchenView==nil) {
            kitchenView=[[KitchenViewController alloc] initWithNibName:@"KitchenViewController_ipad" bundle:nil];
        }
        if (orderView==nil) {
            orderView=[[OrderHistoryController alloc] initWithNibName:@"OrderHistoryController_ipad" bundle:nil];
        }
        if (analyticView==nil) {
            analyticView=[[AnalyticsViewController alloc] initWithNibName:@"AnalyticsViewController_ipad" bundle:nil];
        }
        if (messageView==nil) {
            messageView=[[MessageViewController alloc] initWithNibName:@"MessageViewController_ipad" bundle:nil];
        }
        if (adimnView==nil) {
            adimnView=[[AdminViewController alloc] initWithNibName:@"AdminViewController_ipad" bundle:nil];
        }
        if (ranger==nil) {
            ranger=[[RangerViewController alloc] initWithNibName:@"RangerViewController_ipad" bundle:nil];
        }


    }

}

}

0 个答案:

没有答案