2013-12-12 40 views
1

我有一個應用程序,包含在應用程序的每個視圖控制器中包含一個可重用的XIB。自定義UIView XIB(沒有導航控制器)內的iOS導航後退按鈕

此XIB負責渲染目前不是導航欄的頂部視圖。 導航項目不是現在需要的。

但是,基於上下文導航,可能需要用戶能夠返回到上一個視圖,並且後退按鈕是這種情況的要求。

我想讓這個可重複使用的XIB的後退按鈕部分在每個場景中形成頂部視圖 - 但只是有條件地,並非到處都有。

我閱讀文檔和其他關於SO的問題,告訴我UIBarButtonItem只與nav-controller一起存在。

我覺得這對我來說是矯枉過正,因爲:

1)我不需要後退按鈕處處

2)我不需要導航欄作爲頂欄的外觀。我的頂欄已經在我的XIB中設計了。

我應該改變什麼?

  • 我的XIB?如果我可以在這裏爲後退按鈕創建插座,並根據我所在的視圖控制器隱藏它們,這將是非常受歡迎的解決方案。
  • 我的XIB視圖.m文件?
  • 我的視圖控制器代碼,我想要後退按鈕?

有關代碼的示例將非常感謝。

回答

1

創建一個基本的UIViewController並不是一個很好的選擇,因爲我只是想要在幾個屏幕中使用這個後退按鈕,所以沒有爲整個基類準備它。

最後,我所做的是不可避免的:在導航控制器中嵌入第一個視圖控制器。 然後從這個第一個VC推送段到所有其他VC。

因爲我不想標準導航欄,我做了以下內容:

[self.navigationcontroller setnavigationbarhidden:YES]; 

對於後退按鈕,我添加了一個自定義的UIButton我自己的自定義XIB觀點,躲到/在一個給定的顯示了它有條件視圖控制器。

上的後退按鈕,其中正在顯示它,我添加將執行以下的目標:

[self.navigationController popViewControllerAnimated:YES]; 
1

您可以簡單地將按鈕添加到xib。並實現在視圖控制器類按鈕操作如下:

- (IBAction) backButtonAction 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
1

所有你要去哪裏添加導航欄中的viewcontrollers創建一個共同的基礎視圖控制器。在baseclass中添加XIB並在基類中創建一個函數來啓用和禁用後退按鈕。

MyBaseClassVC.h

@interface MyBaseClassVC : UIViewController 

@property (nonatomic, strong) UIView *titleBarView; 

- (void) enableBackButtonInTitleBar; 

//you can customize your back button with diff image and selector 
- (void) enableBackButtonInTitleBarWithImageName:(NSString*)imageName andSelectorName:(NSString*)selectorName; 

@end 

MyBaseClassVC.m

......

/** 
* Enable the Left or Back Button 
*/ 
- (void) enableBackButtonInTitleBar 
{ 

UIImage *leftBarButtonImage = [UIImage imageNamed:@"Back_btn"]; 
UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
rightBarButton.bounds = CGRectMake(0, 20, leftBarButtonImage.size.width, leftBarButtonImage.size.height); 
[rightBarButton setImage:leftBarButtonImage forState:UIControlStateNormal]; 
[rightBarButton addTarget:self action:@selector(backToPreviousPage) forControlEvents:UIControlEventTouchUpInside]; 

[topbarview addsubview rightBarButton]; 
} 

.....

在其他的ViewController子類此視圖 - 控制。

0

對於這種情況,您需要考慮設計應用程序類。我給你一個非常簡單的例子,它會讓你想到一個叫做「繼承」的OOP概念。

你可以調整代碼來處理你的頭(我。e XIB)

我將創建一個基類btBaseViewController.h & btBaseViewController.m。這個類將具有最常見的功能,我希望我所有的視圖控制器都有這樣的效果。

  1. 帶按鈕的自定義標題視圖。
  2. 內容視圖。
  3. renderUI的一種方法。
  4. 另一種啓用/禁用backButton的方法。

在文件btBaseViewController.h使它像這樣。

#import <UIKit/UIKit.h> 

@interface btBaseViewController : UIViewController 

@property(nonatomic, retain) btHeader *headerView; 

-(void)showHeaderViewWithBackButton:(BOOL)backButton; 

@end 

btBaseViewController.m實現方法

-(void)showHeaderViewWithBackButton:(BOOL)backButton { 
    // Now implement this method in a way to show the backbutton or not depending on 
    [headerView.backButton setAlpha:backButton]; 
} 

現在讓我們假設你有btHomeViewController,獲得從btBaseViewController

#import "btBaseViewController.h" 

@interface btHomeViewController : btBaseViewController 

@end 

在​​在viewdidload寫代碼爲

-(void)viewDidLoad { 
    if(self.navigationController.viewControllers.count >=2) { 
    // it means you have some thing on viewcontroller & you can go back 
     [self showHeaderViewWithBackButton:YES]; 
    } 
} 

我盡我所能來投影解決方案。你需要更多地調整它。