2011-11-28 67 views
0

首先:對不起我的英文不好!使用UIButtons自定義TabBar

我有一個創建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

如果您收到一個無法識別的選擇錯誤,你可以嘗試:

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

注意後面的行動冒號:@selector(SWITCHVIEW :)