2013-02-04 30 views
0

我在當前的UIViewController中添加了UIView。當觸摸屏幕上的任何地方時,我需要刪除UIView。我在UIView中有UIButton。但是,如果我點擊UIButton,那麼也刪除UIView在touchevent上刪除UIView

其實我在UIView中添加了一個按鈕(退出),點擊時應該關閉currentview並去homeViewController。但是,如果我點擊當前視圖上的任何地方,而不是UIView上的按鈕,則應刪除UIView。但在我的情況下,如果我點擊按鈕然後UIView被刪除。

logout= [[UIView alloc]initWithFrame:CGRectMake(210,lbllogin.frame.origin.y+lbllogin.frame.size.height, 80, 50)]; 
logout.backgroundColor = [UIColor yellowColor]; 
btnSignout = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnSignout addTarget:self 
       action:@selector(aMethod:) 
     forControlEvents:UIControlEventTouchDown]; 
btnSignout = CGRectMake(0,0,80,13); 
[logout addSubview: btnSignout]; 
[self.view addSubview:logout]; 


     UIButton *btnsignout=[[UILabel alloc]init]; 
     [lblsignout setFrame:CGRectMake(0, 0, 80, 13)]; 
     lblsignout.textAlignment = NSTextAlignmentLeft; 
     lblsignout.backgroundColor = [UIColor clearColor]; 
     lblsignout .font=[UIFont fontWithName:@"Helvetica-Bold" size:14]; 
     [email protected]"Sign out"; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [logout removeFromSuperview]; 

} 

回答

1

,你必須追蹤觸摸的位置,這意味着你必須檢查你是按鈕的幀是否已經利用。如果您點擊了按鈕的位置(即按鈕框),則不要調用removeFromSuperView。否則調用removeFromSuperView。

//pseudo code,not actual code 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

if(touches.x & touches.y is in button frame) 
{ 
[logout removeFromSuperview]; 
} 
else 
{ 
[btnSignOut removeFromSuperview]; 
} 

} 
+0

是u能告訴我怎麼樣? –

0

爲了增加這個功能,你必須去InterfaceBuilder的身份檢查 - 類,並設置類UIControl.and然後通過CTR-拖動視圖控制器的主視圖連接到您的.h文件中和的內心創造的觸摸動作。像:

- (IBAction)backgroundTapped:(id)sender; 

和.m文件

- (IBAction)backgroundTapped:(id)sender { 
     //remove your view here 
    [logout removeFromSuperview]; 
    }