2010-12-11 23 views
11

Ứng dụng này tôi đang viết có sự cố.UITabBarItems trong UITabBar hiển thị sau khi tôi nhấp vào mục không khi ứng dụng khởi chạy

Tôi đang thiết lập UITabBar trong cửa sổ ứng dụng của mình và đặt các biểu tượng trong tệp chế độ xem. Nhưng khi tôi chạy ứng dụng, các biểu tượng đầu tiên xuất hiện (vì chế độ xem được tải, tôi đoán) và các biểu tượng khác không hiển thị cho đến khi tôi nhấp vào chúng.

Tôi có cần triển khai self.tabBarItem bằng một số phương pháp khác không viewDidLoad?

Cảm ơn trước tất cả mọi người!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    tabBar = [[UITabBarController alloc] init]; 

    SubscriptionsController *subscriptionsController = [[SubscriptionsController alloc] init]; 
    FavoritesController *favoritesController = [[FavoritesController alloc] init]; 
    CategoriesController *categoriesController = [[CategoriesController alloc] init]; 
    TagsController *tagsController = [[TagsController alloc] init]; 
    HelpScreenController *helpScreenController = [[HelpScreenController alloc] init]; 

    tabBar.viewControllers = [NSArray arrayWithObjects: 
     subscriptionsController, 
     favoritesController, 
     categoriesController, 
     tagsController, 
     helpScreenController, 
     nil 
     ]; 

    [window addSubview:tabBar.view]; 

    // Override point for customization after application launch. 
    [window makeKeyAndVisible]; 
    return YES; 
} 

//The View 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    tabIcon = [[UITabBarItem alloc] initWithTitle:@"Abonime" image:[UIImage imageNamed:@"subscr.png"] tag:0]; 
    self.tabBarItem = tabIcon; 
    [tabIcon release]; 
} 

Trả lời

12

Tôi nghĩ bạn nên thiết lập thuộc tính tabBarItem trong initializer được một cái nhìn khiển của (đánh giá từ mã của bạn, nó phải là -init cho mỗi bộ điều khiển). Thực tế, bộ điều khiển thanh tab đủ thông minh để tải chế độ xem theo yêu cầu, tức là thuộc tính tabBarItem phải được đặt trước khi viewDidLoad được gửi.

Ngoài ra, bạn dường như đang làm rò rỉ tất cả các bộ điều khiển chế độ xem. Để khắc phục điều đó, hãy làm như sau:

SubscriptionsController *subscriptionsController = [[[SubscriptionsController alloc] init] autorelease]; 
+0

Cảm ơn rất nhiều! :) nó hoạt động hoàn hảo :) – Olsi

+0

Làm cách nào để thực hiện điều này nhanh chóng 2? – deepakssn

+0

Điều này vẫn giải quyết các vấn đề trong Swift - 7 năm sau đó. – SQLiteNoob

4

Đúng. Các biểu tượng không hiển thị vì chế độ xem (ngoại trừ khung nhìn đầu tiên chưa được tải). Và không được tải cho đến khi bạn nhấn vào một chế độ xem vì viewDidLoad không được gọi cho đến lúc đó.

Tháo mã kiểm tra vào cá nhân UIViewControllers viewDidLoad và Làm điều này ...

NSArray *controllers = [NSArray arrayWithObjects: 
               [NSDictionary dictionaryWithObjectsAndKeys:@"SubscriptionsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil], 
               [NSDictionary dictionaryWithObjectsAndKeys:@"FavoritesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil], 
               [NSDictionary dictionaryWithObjectsAndKeys:@"CategoriesController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil], 
               [NSDictionary dictionaryWithObjectsAndKeys:@"TagsController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil], 
               [NSDictionary dictionaryWithObjectsAndKeys:@"HelpScreenController", @"class", [UIImage imageNamed:@"btn_tax.png"], @"icon", @"Abonime", @"title", nil], 
               nil]; 

NSMutableArray *controllerArray = [NSMutableArray array] ; 

for (NSUInteger i = 0; i < [controllers count]; i++) 
{ 
    id newClass = [[NSClassFromString([[controllers objectAtIndex:i] objectForKey:@"class"]) alloc] init]; 
    UITabBarItem *tabItem = [[UITabBarItem alloc] init]; 
    tabItem.image = [[controllers objectAtIndex:i] objectForKey:@"icon"]; 
    tabItem.title = [[controllers objectAtIndex:i] objectForKey:@"title"]; 
    tabItem.tag = i; 
    [(UIViewController*)newClass setTabBarItem:tabItem]; 
    [tabItem release]; 
    [controllerArray addObject:newClass]; 
    [newClass release]; 
} 

tabBar.viewControllers = controllerArray; 
Các vấn đề liên quan