2016-06-07 23 views
1

我有一個屏幕有導航欄,後退按鈕默認放置在使用navigationItem storyboard。我想根據條件在後退按鈕上彈出視圖控制器。後退按鈕在添加事件之前是默認的。請爲此問題提出解決方案。 enter image description here在沒有UINavigationBar標題的後退按鈕上添加提醒iOS

+1

的[當按下的UINavigationController的背面欄按鈕執行動作]可能的複製(http://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button被按下) –

回答

0

如果您設置tintColor中導航欄,添加自定義後退按鈕圖像無題是着色顏色將反映圖像的顏色。請按照這個蘋果文檔鏈接。

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/index.html#//apple_ref/doc/uid/TP40012857-UIView-SW7

UINavigationItem *navItem = [[UINavigationItem alloc] init]; navBar.tintColor = self.tintColor; 

UIImage *myImage = [UIImage imageNamed:@"left_arrow.png"]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonFunction:)]; 
navItem.leftBarButtonItem = leftButton; 
navBar.items = @[ navItem ]; 
2

你應該實現此方法,

-(void)willMoveToParentViewController:(UIViewController *)parent{ 

    if (parent == nil) { 

     // handle your back button's task here 
    } 
    } 

這種方法得到的導航之前叫parentview控制器。

如果它不工作你的情況,那麼你可以使用自定義後退按鈕類似,

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)]; 
    self.navigationItem.leftBarButtonItem=newBackButton; 
} 

-(void)goBack:(UIBarButtonItem *)sender { 

     //Handle your back button's task here and you have to call popViewcontroller your self in this case 
} 
+0

我想要顯示警報,然後對警報進行操作,然後彈出視圖控制器。 –

+0

我不想要後面的標題,只有後面的箭頭。 –

+0

然後添加具有清晰顏色的自定義按鈕,並且不要設置標題。只需添加無邊框清晰顏色的按鈕即可。所以原來的導航欄按鈕將可見,並點擊自定義按鈕上的檢測 – Lion

0
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)]; 
self.navigationItem.leftBarButtonItem=newBackButton; 

-(void)goBack:(UIBarButtonItem *)sender { 

     UIAlertView *removeAddAlertView = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@「Are you want to go Back 」 delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil]; 
     [removeAddAlertView show]; 

} 
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
     if (buttonIndex == 0) { 
      // cancel 
     }else if (buttonIndex == 1) { 
      // ok 
     } 
} 
+0

我不想要後標題,只有後面的箭頭。 –

相關問題