casillas
casillas

Reputation: 16813

Custom tabbar button does not work

I have created a custom tabbar and added a custom image on the center position. However, whenever I click on that custom button, it does not take me to the selected item.

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* buttonImage = [UIImage imageNamed:@"icon_floating.png"];
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];

    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }

    [button addTarget:self
               action:@selector(myAction)
     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

-(void) myAction
{
   // the following line is getting called
    self.tabBarController.selectedIndex = 2;
}

enter image description here

Upvotes: 0

Views: 174

Answers (1)

rmaddy
rmaddy

Reputation: 318934

It seems that 'self` is a tab bar controller. Change:

self.tabBarController.selectedIndex = 2;

to

self.selectedIndex = 2;

Upvotes: 2

Related Questions