自定义TabBar与UIButtons

时间:2011-11-28 17:33:21

标签: uibutton instance-methods

第一:抱歉我的英语不好!

我有一个类,我在其中创建UIButton。 UIButton我添加了一个UIView。在ViewController中我想添加这些自定义tabbar。这到目前为止运作良好。但是,addTarget存在问题:action:forControlEvents: - 如何从ViewController中运行类中的实例方法?该类的类型为NSObject。我的代码:

TabBar *tabBar = [[TabBar alloc] init];
    [tabBar tabBarButton];
    [tabBar.home addTarget:tabBar action:@selector(switchView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tabBar.tabView];

这是我的班级:

@implementation TabBar
@synthesize tabView, home, list, charts, favorits, more;

-(void)tabBarButton{

    tabView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320,56)];

    home = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 56)];
    list = [[UIButton alloc] initWithFrame:CGRectMake(63, 0, 40, 56)];
    charts = [[UIButton alloc] initWithFrame:CGRectMake(123, 0, 49, 56)];
    favorits = [[UIButton alloc] initWithFrame:CGRectMake(192, 0, 66, 56)];
    more = [[UIButton alloc] initWithFrame:CGRectMake(278, 0, 42, 56)];

    [home setImage:[UIImage imageNamed:@"home.png"] forState:UIControlStateNormal];
    [list setImage:[UIImage imageNamed:@"liste.png"] forState:UIControlStateNormal];
    [charts setImage:[UIImage imageNamed:@"charts.png"] forState:UIControlStateNormal];
    [favorits setImage:[UIImage imageNamed:@"favorites.png"] forState:UIControlStateNormal];
    [more setImage:[UIImage imageNamed:@"more.png"] forState:UIControlStateNormal];

    home.tag = 1;
    list.tag = 2;
    charts.tag = 3;
    favorits.tag = 4;
    more.tag = 5;


    [tabView addSubview:home];
    [tabView addSubview:list];
    [tabView addSubview:charts];
    [tabView addSubview:favorits];
    [tabView addSubview:more];

}

-(void)switchView{
    NSLog(@"hi");
}

当我启动应用程序时,按下一个按钮时会立即崩溃。

1 个答案:

答案 0 :(得分:1)

如果您收到无法识别的选择器错误,可以尝试:

[tabBar.home addTarget:tabBar action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];

注意冒号后面的冒号:@selector(switchView :)

相关问题