2013-07-22 30 views
0

我想在主視圖上創建一個子視圖。我想使用視圖,但意見混淆層

在此子視圖中添加web視圖和退出按鈕。

我通過創建一個圓圈和一個標籤通過視圖創建退出按鈕圖像。

下面是我的代碼,我的按鈕操作即退出按鈕的功能不能執行。

這是什麼問題?對於同一個按鈕上一個動作

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
if (self) { 


    self.frame = CGRectMake(0, 0, 1024, 768); 

    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]]; 


    UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(8,20,40,40)]; 
    circleView.alpha = 0.5; 
    circleView.layer.cornerRadius = 20; 
    //self.circleView.backgroundColor = [UIColor blueColor]; 


    circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 
    circleView.layer.borderColor = [[UIColor blackColor] CGColor]; 
    circleView.layer.borderWidth = 5; 

    UILabel* circleIndex = [[UILabel alloc] init]; 
    circleIndex.frame = CGRectMake(13, 10, 25, 20); 
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:22]]; 
    [circleIndex setText:@"x"]; 

    [circleView addSubview:circleIndex]; 




    UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(40, 80, 944, 600)]; 

    UIWebView* webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 944, 600)]; 

    NSString *[email protected]"http://www.google.com"; 
    NSURL *nsurl=[NSURL URLWithString:url]; 
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl]; 
    [webView loadRequest:nsrequest]; 


    [alertView addSubview:webView]; 

    UIButton *exit = [[UIButton alloc]init]; 
    exit.frame = CGRectMake(920, -40, 38, 38); 
    [exit addTarget:self action:@selector(exitTheWebView) forControlEvents:(UIControlEventTouchUpInside)]; 
    [exit addTarget:self action:@selector(exitTheAlert) forControlEvents:(UIControlEventTouchUpInside)]; 
    [exit setBackgroundColor:[UIColor redColor]]; 

    // exit setBackgroundImage:<#(UIImage *)#> forState:<#(UIControlState)#> 

    [circleView addSubview:exit]; 

    [alertView addSubview:circleView]; 


    [self addSubview:alertView]; 

} 
return self; 
} 



-(void)exitTheWebView{ 

[self removeFromSuperview]; 

NSLog(@"bitch"); 
[self release]; 


} 

回答

0
[exit addTarget:self action:@selector(exitTheWebView) forControlEvents:(UIControlEventTouchUpInside)]; 
[exit addTarget:self action:@selector(exitTheAlert) forControlEvents:(UIControlEventTouchUpInside)]; 

兩種方法,第一種方法將不會執行,因爲第二個重寫的第一個動作

+0

這只是一個錯誤,我忘了刪除該行。它不是實際的問題 – erdemgc

0

要添加到像相同的按鈕操作:

[exit addTarget:self action:@selector(exitTheWebView) forControlEvents:(UIControlEventTouchUpInside)]; 
[exit addTarget:self action:@selector(exitTheAlert) forControlEvents:(UIControlEventTouchUpInside)]; 

所以它會在你點擊它時調用第二個動作。手段(exitTheAlert

那個按鈕的代碼也變更爲:

UIButton *exits = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
exits.frame = GRectMake(920, -40, 38, 38);; 
exits addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside]; 
[circleView addSubview:exits]; 
[circleView bringSubviewToFront:exits] 
[circleView setUserInteractionEnabled:YES]; 

注:exit是一個在建的關鍵字,所以不要使用關鍵字的變量/對象。

+0

問題是實際上一個圖層問題,一些視圖是在彼此的頂部,防止處理的行動。但我無法弄清楚。 – erdemgc

+0

@erdemgc:添加說明,請立即檢查 –