自定义标签栏按钮不起作用

时间:2016-12-12 04:10:00

标签: ios objective-c tabbar

我创建了一个自定义标签栏,并在中心位置添加了自定义图像。但是,每当我点击该自定义按钮时,它都不会将我带到所选项目。

- (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

1 个答案:

答案 0 :(得分:2)

似乎&#39; self`是一个标签栏控制器。变化:

self.tabBarController.selectedIndex = 2;

self.selectedIndex = 2;