2017-08-25 88 views
1

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

這裏這裏的結果似乎,注意工作,以及非常愚蠢的底部面板,但它顯示了:

enter image description here 但是,當按下左右按鈕時,沒有打印日誌,它預計會指示該按鈕有效,我做了什麼錯了?謝謝

+0

嘗試替換子視圖的代碼https://stackoverflow.com/a/13617590/4601900並將顏色也設置爲按鈕以測試 –

+0

爲什麼您創建了UIViewController類,您是添加UIView。 –

+0

感謝您的回覆@MikeAlter已嘗試爲按鈕添加顏色,並且其結果非常符合預期,它會檢查您提供的鏈接以獲取更多信息。 – armnotstrong

回答

0

您在視圖中首先添加按鈕,然後添加底部視圖。 如果這是你正在做的事情,那麼它可以被底部視圖重疊。 在這種情況下,按鈕不能被輕敲,因爲它們的頂部有底視圖。 所以請確保你先添加底部視圖然後按鈕。 (或更改其框架以避免重疊)

+0

''setUpButtons'在'viewDidLoad'中被調用,所以我想也許按鈕的添加順序是好的? – armnotstrong

+0

先添加bottomView,然後添加按鈕 –