我想創建一個底部的選項卡,用戶按下時可以更改視圖。子視圖中的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