0
votes

Here i am trying to dynamically add and remove the tab from uitabbarcontroller .

As well as replace them . My apps situation is as below.

First view is Logon and about

After successful logon new 3 tabs . With last as More and two other .

In more Logoff option is there after selecting this i want to remove the last tab and

replace first two with again logon and About. i.e (Home View)

So in my code i m doing like this ........

///AppDelegate.m

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window.rootViewController = tabBarCntr; tabBarCntr=[[UITabBarController alloc]init]; LogonViewController *logon=[[LogonViewController alloc]init]; logon.tabBarItem.title=@"Logon"; AboutViewController *about=[[AboutViewController alloc]init]; about.tabBarItem.title=@"About"; tabBarCntr.viewControllers=[NSArray arrayWithObjects:sos1,about,nil]; [logon release]; [about release];

    [self.window addSubview:tabBarCntr.view];

    [self.window makeKeyAndVisible]; }

and in my IBAction after logon successful setting new viewcontrollers to tabbarcontroller

self.tabBarController.viewControllers=[NSArray

arrayWithObjects:newController1,newController2,more,nil];

    Now in my loggoff i coding something like.....
       NSMutableArray *newArray;   
     newArray=[NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
       [newArray replaceObjectAtIndex:0 withObject:logon];
       [newArray replaceObjectAtIndex:1 withObject:about];
       [newArray removeLastObject];
       self.tabBarController.selectedIndex=0;
       [self.tabBarController setViewControllers : newArray];


  but my applcation crash here after this ....
 can any has solution for this ...

Thnx in advance.

Paggyyyyy

Edit: NSMutableArray arrayWithArray added space to fix code

2

2 Answers

2
votes

In my opinion, you use 2 separate tabBarControllers. one for login and other for app. once you login remove & release the login tab and add app tab. And again after logout remove & release app tab and add the login tab. This will be easier to access tabs, instead of removing/adding viewcontrollers, renaming tabs, changing tab images.

0
votes

You can modify tab bar controller's by property setViewControllers:animated:YES. You can assign a new array there. It switches accordingly.

[tabBarController setViewControllers:array animated:YES];

There is a one important gotcha. I use manual reference counting however I used autorelease option for view controllers assigned to this array as I used Xcode template as an application basis. These are retained only by that array then. If you forget retain all view controllers which might appear within different tab arrays during an application lifecycle, curious memory crash appears after change, during change or even array initialization. Therefore it is necessary retain your references to these view controllers/