我想创建一个底部的选项卡,用户按下时可以更改视图。子视图中的UIButton不响应使用objective-c触摸事件
要做到这一点我创建视图控制器的视图,并设置它在init
这里框底部面板视图控制器
@implementation BottomTabViewController
-(id) initWithFrame:(CGRect)frame{
if(self = [super init]){
self.view.frame = frame;
return self;
}else{
return nil;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpButtons];
// Do any additional setup after loading the view.
}
-(void) featureBtnClick:(id*) sender{
NSLog(@"featureBtn Clicked");
}
-(void) favBtnClick:(id*)sender{
NSLog(@"Fav Btn Clicked");
}
-(void) setUpButtons{
UIButton *features = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[features setBackgroundImage:[UIImage imageNamed:@"feature.png"] forState:UIControlStateNormal];
features.backgroundColor = [UIColor greenColor];
[features addTarget:self action:@selector(featureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:features];
UIButton *favBtn = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width - 100, 10, 50, 50)];
[favBtn setBackgroundImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
[favBtn addTarget:self action:@selector(favBtnClick:) forControlEvents:UIControlEventTouchUpInside];
favBtn.backgroundColor = [UIColor greenColor];
[self.view addSubview:favBtn];
}
@end
我补充说,我有这样的代码视图:
-(void) setUpBottom{
BottomTabViewController *botm = [[BottomTabViewController alloc]initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 55, [[UIScreen mainScreen]bounds].size.width, 55)];
botm.view.backgroundColor = [UIColor redColor];
// botm.view.userInteractionEnabled = false;
botm.view.userInteractionEnabled = true;
[self.view addSubview:botm.view];
}
这里这里的结果似乎,注意工作,以及非常愚蠢的底部面板,但它显示了:
但是,当按下左右按钮时,没有打印日志,它预计会指示该按钮有效,我做了什么错了?谢谢
尝试替换子视图的代码https://stackoverflow.com/a/13617590/4601900并将颜色也设置为按钮以测试 –
为什么您创建了UIViewController类,您是添加UIView。 –
感谢您的回复@MikeAlter已尝试为按钮添加颜色,并且其结果非常符合预期,它会检查您提供的链接以获取更多信息。 – armnotstrong