2010-12-11 5 views
11

Diese Anwendung, die ich schreibe, hat ein Problem.UITabBarItems in UITabBar anzeigen, nachdem ich auf das Element nicht beim Start der Anwendung geklickt habe

Ich bin die UITabBar in meinem Anwendungsfenster einrichten und legen Sie die Symbole in den Ansichtsdateien. Aber wenn ich die App starte, erscheinen die ersten Symbole (weil die Ansicht geladen ist, nehme ich an) und die anderen Symbole werden erst angezeigt, wenn ich auf sie klicke.

Muss ich self.tabBarItem in einer anderen Methode implementieren, nicht viewDidLoad?

Vielen Dank im Voraus an alle!

- (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]; 
} 

Antwort

12

Ich denke, Sie sollten die tabBarItem Eigenschaft in einer View-Controller designierten Initialisierer (Beurteilung aus dem Code, es -init für jede der Steuerungen sein müssen) eingestellt. In der Tat ist der Tab-Leiste-Controller intelligent genug, um die Ansichten bei Bedarf zu laden, das heißt, die tabBarItem-Eigenschaft sollte gesetzt werden, bevor viewDidLoad gesendet wird.

Außerdem scheinen Sie alle View-Controller undicht zu sein. Um das zu beheben, gehen Sie folgendermaßen vor:

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

Vielen Dank! :) es hat einwandfrei funktioniert :) – Olsi

+0

Wie mache ich das in swift 2? – deepakssn

+0

Dies löst noch Probleme in Swift - 7 Jahre später. – SQLiteNoob

4

Korrekt. Die Symbole werden nicht angezeigt, da die Ansicht (abgesehen von der ersten, noch nicht geladen ist). Und wird erst geladen, wenn Sie auf eine Ansicht tippen, weil viewDidLoad erst dann aufgerufen wird.

den Code in den einzelnen UIViewControllers viewDidLoad entfernen und diese Do ...

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;