0

所以我有一個UIViewController(主應用程序控制器是一個TabBarController)。在這裏有一個UINavigationBar和一個UIBarButtonItem。我很清楚我在Interface Builder中正確連接了所有東西,並且代碼中的插座連接到了.xib中的按鈕。應該是因爲該方法正常工作。隱藏兩個UIViews中的第一個按鈕,但它在第二個可見

現在我在這個視圖上還有另一個按鈕,它帶來了第二個視圖,一個UIWebView。我想要這個標記爲「Back」的UIBarButtonItem使UIWebView消失,並且帶回第一個UIView,它正確地做了。但是,當你在第一個UIView上時,不需要看到UIBarButtonItem,所以我怎麼能隱藏它,然後把它提交給UIWebView。順便說一下,這兩個視圖使用相同的UINavigationBar,UIWebView在標籤欄和導航欄內引入。

這裏是我的代碼:

#import "WebViewController.h" 

@implementation WebViewController 
@synthesize webButton; 
@synthesize item; 
@synthesize infoView; 

UIWebView *webView; 


+ (UIColor*)myColor1 { 
    return [UIColor colorWithRed:0.0f/255.0f green:76.0f/255.0f blue:29.0f/255.0f alpha:1.0f]; 
} 

// Creates Nav Bar with default Green at top of screen with given String as title 
+ (UINavigationBar*)myNavBar1: (NSString*)input { 

    UIView *test = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, test.bounds.size.width, 45)]; 
    navBar.tintColor = [WebViewController myColor1]; 

    UINavigationItem *navItem; 
    navItem = [UINavigationItem alloc]; 
    navItem.title = input; 
    [navBar pushNavigationItem:navItem animated:false]; 

    return navBar; 
} 

- (IBAction) pushWebButton { 

    self.navigationItem.rightBarButtonItem = item; 

    CGRect webFrame = CGRectMake(0.0, 45.0, 320.0, 365.0); 
    webView = [[UIWebView alloc] initWithFrame:webFrame]; 

    [webView setBackgroundColor:[UIColor whiteColor]]; 
    NSString *urlAddress = @"http://www.independencenavigator.com"; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    webView.scalesPageToFit = YES; 
    [webView loadRequest:requestObj]; 

    [self.view addSubview:webView]; 
    [webView release]; 

} 

- (void) pushBackButton { 

    [webView removeFromSuperview]; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 

- (void)viewDidLoad 
{ 
    self.navigationItem.rightBarButtonItem = nil; 

    [super viewDidLoad]; 
} 

@end 

任何人都知道嗎?

回答

1

編輯︰這個答案不適用於nil,但我留在這裏,因爲它確實工作,當你想暫時用另一個按鈕替換後退按鈕。在評論中看到正確的答案。

您可以嘗試這樣的事:

在一個App我的工作有,我想暫時換後退鍵取消按鈕的情況下,

所以我節省了指向它的指針:

tempButtonItem = self.navigationItem.leftBarButtonItem;

變化navigationItem.leftBarButtonItem到取消按鈕: self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease];

再後來,當我想有後退按鈕,我再次恢復它: self.navigationItem.leftBarButtonItem = self.tempButtonItem;

+0

奇怪的是,即使我的欄按鈕項設置爲nil這樣的: self.navigationItem.rightBarButtonItem =零; 無論如何它仍然出現。我甚至在viewDidLoad上設置它爲零,就像界面生成器沒有聽代碼。 – Scott 2010-03-19 00:33:25

+0

我想我從來沒有注意到,因爲我用另一個按鈕替換了後退按鈕。你可以試試這個:self.navigationItem.hidesBackButton = YES; – 2010-03-19 02:15:05

+0

仍然不能正常工作我在這一點上沒有線索。 In viewDidLoad I do self.navigationItem.leftBarButtonItem = nil; 後退按鈕方法在其中具有相同的代碼。我嘗試通過代碼生成按鈕,所以在web按鈕上單擊我做 self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed)] autorelease]; – Scott 2010-03-19 04:23:39

相關問題