Why TabBar code is different for each controller


In the chapter they had you create a TabBar for the hypnosis view as well as the reminder view. However, setting the tab bar title and image were done in different ways. The two ways are below:

self.tabBarItem.title = @" Hypnotize";
UIImage *i = [UIImage imageNamed:@" Hypno.png"];
self.tabBarItem.image = i;

UITabBarItem *tbi = self.tabBarItem;
tbi.title = @" Reminder";
UIImage *i = [UIImage imageNamed:@“Time.png”];
self.tabBarItem.image = i;

My question is why these methods are different. I tried using the same for each and it worked fine. Was this just to show another way to handle this or is there another reason for this code to be different for each view?


The code is actually the same. In the ReminderViewController you create a local variable tbi to point to the object referred to by self.tabBarItem. Then on the next line you are setting the title of self.tabBarItem.


I get is the “same” code, but which way is recommended using in those cases?


I too would like to know if there was a reason for doing this, aside from showing 2 “different” ways of writing the same thing.

I imagine either way is fine, and that most people will not create the UITabBarItem, just because it is more streamlined once you are familiar with “nested” dot notation.

Anyone care to chime in?


No difference at all. My answer here explains a bit: viewtopic.php?f=487&t=8240

To the question of why they are different, just an oversight in consistency. Whoops! Either way works fine, but if line lengths start to get long, that is when I start to break out variables into separate lines, as it’ll often help the code be more readable.