我试图使用视图,但层次上的视图混乱

时间:2013-07-22 13:22:20

标签: ios view uiview

我正在尝试在主视图上创建子视图。

在此子视图中添加网页视图和退出按钮。

我通过创建圆圈和标签来创建退出按钮图像。

在我的代码下面,我的按钮操作即退出按钮的功能无法执行。

这有什么问题?

- (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 *url=@"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];


}

2 个答案:

答案 0 :(得分:0)

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

对同一个按钮执行一个操作的两种方法,第一个方法将不会执行,因为第二个方法会重写第一个操作

答案 1 :(得分: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是内置关键字,因此请勿对变量/对象使用关键字。