I have the following buttons:
// First Tab selected
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTabActive.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
// First Tab
UIButton *firstTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
firstTabButton.frame = CGRectMake(75, 42, 153, 42);
[firstTabButton setTitle:@"FirstTab selected" forState:UIControlStateNormal];
[firstTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[firstTabButton setBackgroundImage:[UIImage imageNamed:@"FirstTab.png"] forState:UIControlStateNormal];
firstTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:firstTabButton];
//Second Tab Active
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab Active" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
//Second Tab
UIButton *secondTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
secondTabButton.frame = CGRectMake(75, 42, 153, 42);
[secondTabButton setTitle:@"Second Tab" forState:UIControlStateNormal];
[secondTabButton addTarget:self action:@selector(tabToggler:) forControlEvents:UIControlEventTouchUpInside];
[secondTabButton setBackgroundImage:[UIImage imageNamed:@"SecondTabActive.png"] forState:UIControlStateNormal];
secondTabButton.adjustsImageWhenHighlighted = NO;
[self.view addSubview:secondTabButton];
appDelegate = [[UIApplication sharedApplication] delegate];
For the first time I’d like to display First tab Active and the 2nd tab.
What the tabToggler method will look like? How do I proceed?
You should just use 2 instances of UIButton and have them declared as properties/ivars in your .h file. Then just switch the properties that make a button look active/inactive in your toggleActiveTab method.
Like so:
In .h
In .m (loadView or viewDidLoad, depending on whether you use a NIB or not):
Finally, your toggleActiveTab method might look something like this:
Don’t forget to release the 2 buttons in your dealloc method.
Also: Have you considered using a UITabBarController instead?