2011-07-12 89 views
1

工作的一個應用程序,並使用下面的代碼: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]; 
} 

出於某種原因,第一次應用程序運行書籤按鈕將不會對水龍頭回應,但如果我把一個視圖控制器然後返回它的工作正常。花了一段時間試圖弄清楚這一點,沒有任何工作。

任何想法???

謝謝!

+0

關於bookmarkButtonTapped方法的任何不尋常的事情? –

+0

不,在它上面設置一個斷點,它不會被調用。它只是將視圖控制器推到nab控制器上。 – Marky

回答

0

道歉,這是(像往常一樣!)一個愚蠢的錯誤!

在委託我設置:

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

,且必須忘了這回事。將導航欄設置爲隱藏,意味着導航欄不會阻止按鈕。

感謝您的幫助!

0

,您是否試圖

[contentsHeaderImageView bringSubViewToFront:self.bookmarkButton]; 

也headerView創建一個UIView實例,並添加ImageView的和按鈕到視圖。

+0

謝謝,我試過了,它沒有工作 – Marky

+0

啊!如果我將按鈕向下移動50px,它將起作用。我認爲這與隱藏的網吧有關。 – Marky