第一:对不起我的英语不好!
我有一个创建 UIButton 的类。UIButton 我添加了一个 UIView。在 ViewController 我想添加这些自定义标签栏。到目前为止,这运作良好。但是,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");
}
当我启动应用程序时,当我按下一个按钮时它会立即崩溃。