UIButton没有回应。为什么?

时间:2011-07-12 02:43:34

标签: iphone uibutton

使用以下代码处理应用程序:

- (void)viewDidLoad {
    [super viewDidLoad];

    dataSource = [[NSArray alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"stories.plist"]];

    contentsHeaderImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ContentsHeader.png"]] autorelease];
    [contentsHeaderImageView setFrame:CGRectMake(0, 0, 320, 131)];
    [contentsHeaderImageView setUserInteractionEnabled:YES];
    [self.view addSubview:contentsHeaderImageView];

    UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(contentsHeaderImageView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(contentsHeaderImageView.frame))] autorelease];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
    [tableView setContentInset:UIEdgeInsetsMake(1, 0, 1, 0)];
    [tableView setSeparatorColor:[UIColor whiteColor]];

    [self.view addSubview:tableView];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [tableView setBackgroundColor:[UIColor clearColor]];


    UIImageView *newView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fade.png"]] autorelease];
    [newView setFrame:CGRectMake(0, -20, 320, 69)];
    [self.view addSubview:newView];


    splashImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]] autorelease];
    [splashImage setFrame:CGRectMake(0, -20, 320, 480)];
    [self.view addSubview:splashImage];

    [self performSelector:@selector(animateOutSplashImage:) withObject:splashImage afterDelay:3];

}

-(void)drawBookmarkButton
{
    if (self.bookmarkButton) {
        [self.bookmarkButton removeFromSuperview];
    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"bookmark"]) {
        self.bookmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bookmarkButton setImage:[UIImage imageNamed:@"bookmark 1.png"] forState:UIControlStateNormal];
        [contentsHeaderImageView addSubview:self.bookmarkButton];
        [self.bookmarkButton addTarget:self action:@selector(bookmarkButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        [self.bookmarkButton setFrame:CGRectMake(260, 0, 50, 35)];


        //UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bookmarkButtonTapped)] autorelease];
        //[self.bookmarkButton addGestureRecognizer:gr];
    }
}

-(void)animateOutSplashImage:(UIImageView *)splashImg
{
    [UIView animateWithDuration:1 animations:^{
        [splashImage setAlpha:0];

    }completion:^(BOOL finished){
        [splashImage removeFromSuperview];
        splashImage = nil;
        //[self drawBookmarkButton];
    }];

}

-(void)viewWillAppear:(BOOL)animated
{
    [self drawBookmarkButton];
}

出于某种原因,第一次运行应用程序时,书签按钮不会响应水龙头,但是如果我按下一个视图控制器,那么返回它的工作正常。花了一段时间试图解决这个问题,没有任何工作。

任何想法???

谢谢!

2 个答案:

答案 0 :(得分:0)

你试过

吗?
[contentsHeaderImageView bringSubViewToFront:self.bookmarkButton]; 

还为headerView创建一个UIView实例,并将imageView和按钮添加到视图中。

答案 1 :(得分:0)

道歉,这是(和我一样!)一个愚蠢的错误!

在我设置的代表中:

self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;

并且一定忘记了。将导航栏设置为隐藏意味着导航栏没有阻止按钮。

感谢您的帮助!