replacement for tabbar selected index

200 views Asked by At

I have an old app where I have 5 tabs. Each tab have list of ads & there is details. Also each tab have respective add ad. Design for all 5 tabs is same except some changes, so I decided to use 1 screen only for all 5 tabs.

Now what I am doing is while add ad, I am checking which tab I am and based on tab bar index, I am showing hiding fields. Same is applicable for details screen also. Sample code is as shown below.

if (self.tabBarController.selectedIndex==0) {
    field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==1) {
    field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==2) {
    field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==3) {
    field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==4) {
    field1.hidden = YES;
}

Now I have around 15 fields for each form so I write this code for all fields.

What I noticed that client change the tab bar position continuously so I was looking for efficient way of doing it.

Right now what I do is at all places manually change index positions by doing Search-Replace, however I dont like this.

I was looking for a way where instead of selectedIndex, I was looking for someconstant value that I will assign to tab item of tab bar, so my code will change as below

if (self.tabBarController.selectedIndex==adType_News) {
    field1.hidden = NO;
} else if (self.tabBarController.selectedIndex==adType_Occasions) {
.
.
And so on...

This way, I only need to change in the storyboard and code level will not have any changes.

Is there way where I can achieve this?

Main Issue

As client ask to change tab bar completely, I need to make changes of selectedIndex changes at all places which I don't like and feel more in-efficient way. So I was looking for a way where I will make change in storyboard only and coding level there won't be any change.

The main issue for me is changing selectedIndex in all files which make more error.

1

There are 1 answers

9
danh On BEST ANSWER

I think I understand the question to mean that the code is full of number literals, referring to the selected index of the tabbar, and the business requirements about the ordering of items are shifting. Fix by changing the literals to something symbolic that can be controlled centrally:

// in a new file, indexes.h
#define INDEXA  (0)
#define INDEXB  (1)
#define INDEXC  (2)

// wherever existing code refers to the tab selection
#import "indexes.h"

// in the code, for example if selectedIndex == 2, change to
if (self.tabBarController.selectedIndex==INDEXC) {
    // ...