开发者

UIButton not responding. Why?

working on an app and using the following code:

- (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 remov开发者_JAVA百科eFromSuperview];
        splashImage = nil;
        //[self drawBookmarkButton];
    }];

}

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

For some reason the first time the app is run the bookmark button won't respond to taps, but if I push a view controller then go back it's working fine. Spent a while trying to figure this out and nothing is working.

Any ideas???

Thanks!


did you try

[contentsHeaderImageView bringSubViewToFront:self.bookmarkButton]; 

also create a UIView instance for headerView and add imageView and button into the view.


Apologies, it was (as usual with me!) a stupid mistake!

In the delegate I set:

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

and must have forgot about it. Setting the nav bar to hidden instead meant that the nav bar wasn't blocking the button.

Thanks for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜