2011-07-07 49 views
0

以下是我的編碼如何用不同的圖像完全替換後退按鈕的圖像?

// Navigation logic may go here -- for example, create and push another view controller. 
NextViewController *nextViewController = 
[[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil]; 
[self.navigationController pushViewController:nextViewController animated:YES]; 
[nextViewController release]; 

UIImage *backImage=[UIImage imageNamed:@"backbutton.png"]; 
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc] 
             initWithImage:backImage style:UIBarButtonItemStylePlain target:nil 
             action:nil]; 
self.navigationItem.backBarButtonItem = backBarButtonItem; 
[backBarButtonItem release]; 

} 

回答

1

在這裏,從我自己的項目。 (我在viewDidLoad中這樣做)

UIImage *image = [UIImage imageNamed:@"btn-custom-back.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    [button addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside]; 

    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)]; 
    [buttonView addSubview:button]; 

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView]; 
    [buttonView release]; 

    self.navigationItem.leftBarButtonItem = backItem; 
    [backItem release]; 

而且方法:

- (void)popView { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

感謝您的幫助。我已經使用你的代碼,後退按鈕被完全替換,但它不能正常工作。這意味着當我點擊它時,應用程序就會凍結。 – David

+0

@David:你可以在你想要的地方發佈代碼嗎? – LuckyLuke

+0

我試圖在子類下的註釋//實現viewDidLoad在加載視圖後執行額外的設置,通常是從一個筆尖。 - (void)viewDidLoad {super viewDidLoad]; \t self.title = @「Search」; } – David

1

的後退按鈕修改有你推新視圖控制器到導航堆棧之前完成。

所以只要將你的代碼到處尋找這樣的:

UIImage *backImage=[UIImage imageNamed:@"backbutton.png"]; 
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc] 
            initWithImage:backImage style:UIBarButtonItemStylePlain target:nil 
            action:nil]; 
self.navigationItem.backBarButtonItem = backBarButtonItem; 
[backBarButtonItem release]; 

NextViewController *nextViewController = 
[[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil]; 
[self.navigationController pushViewController:nextViewController animated:YES]; 
[nextViewController release];