簡體   English   中英

調用自定義視圖時 UIButton 不可點擊

[英]UIButton not clickable when custom view called

我是 iPhone 開發的新手,我需要幫助來理解以下內容,因為我能夠使用以下內容創建 newView

UIView *newView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 30)]; 
newView.backgroundColor=[UIColor clearColor];
UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];


newViewBtn.frame = CGRectMake(newView.frame.origin.x+5,
                            newView.frame.origin.y+5,60,20);
[newView addSubview:newViewBtn];
[self.view addSubview:newView]; 

上面的代碼運行沒有任何問題。 但是當我嘗試使用以下方法創建視圖時,視圖創建好了,但視圖上的按鈕不可點擊。

int randNumX = arc4random() % 150;
int randNumY = arc4random() % 200;
UIView newView=[[UIView alloc]init];
newView.frame =CGRectMake(randNumX, randNumY, 80, 30);

newView.backgroundColor=[UIColor clearColor];

UIButton *newViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
newViewBtn.frame  = CGRectMake(newView.frame.origin.x+5
                         ,newView.frame.origin.y+5,60,20);
[newView addSubview:newViewBtn];
[self.view addSubview:newView];

如果更改以下代碼,還有另一種情況

 newViewBtn.frame = CGRectMake(newView.frame.origin.x+5
                          ,newView.frame.origin.y+5,60,20);

使用下面的代碼應用程序崩潰

 newViewBtn.frame =CGRectMake(randNumX+5,randNumY+5,60,20);

任何幫助將不勝感激,提前致謝。

我已經添加了以下代碼

     newViewBtn addTarget:self action:@selector(btnclick:)forControlEvents:UIControlEventTouchUpInside];

     -(void)btnclick:(id)sender
     {
       //my code
     }

它在第一種情況下工作:

我在這里主要關心的是當 newView 被繪制時,為什么這個視圖上的按鈕不可點擊

向 UIButton 添加自定義子視圖時,您必須關閉 userInteractionEnabled 屬性。

customView.userInteractionEnabled = NO;

然后添加到您的按鈕。

[customButton addSubView:customView];

否則,您的自定義視圖會在按鈕之前攔截觸摸事件,因為它已添加到視圖堆棧的頂部。

這里的問題是下面的行,因為新的子視圖必須根據父視圖

    newViewBtn.frame =CGRectMake(randNumX+5,randNumY+5,60,20);

把上面寫成下面解決了這個問題

    newViewBtn.frame =CGRectMake(5,5,60,20);

你需要像這樣添加目標......

   UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
   newViewBtn.frame=CGRectMake(newView.frame.origin.x+5,newView.frame.origin.y+5,60,20);
   [newViewBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside]; // ---- add this line.....
   [newView addSubview:newViewBtn];

   -(void)btnPressed:(id)sender
   {
        NSLog(@"Btn Pressed");
   }

你忘了這個:

 UIButton *newViewBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  [newViewBtn addTarget:self action:@selector(clickeMe) forControlEvents:UIControlEventTouchUpInside];

請試試這個。

我相信您忘記添加目標(單擊按鈕時調用的函數):

 [newViewBtn addTarget:self action:@selector(yourfunction:) forControlEvents:UIControlEventTouchUpInside];

確保您定位的函數存在,否則單擊時會崩潰。

- (void) yourfunction {
    NSLog(@"Click Detected");
}

嘗試以下代碼...

int randNumX = arc4random() % 150;
int randNumY = arc4random() % 200;

UIView newView=[[UIView alloc]init];

newView.frame =CGRectMake(randNumX, randNumY, 80, 30);

newView.backgroundColor=[UIColor clearColor];

UIButton *newViewBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

newViewBtn.frame  = CGRectMake(0,0,60,20);

newViewBtn.center = newView.center;

[newViewBtn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];

[newView addSubview:newViewBtn];

[self.view addSubview:newView];


-(void)btnTapped:(id)sender
{
     NSLog(@"btnTapped");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM