2014-03-05 51 views
2

我使用SWRevealViewController在我的應用程序中添加了側欄菜單。我有我的SideBarViewController我設置視圖控制器,可以說OptionAViewController。我使用顯示視圖控制器segue來打開這個新視圖OptionAViewController。從OptionAViewController我想實現一個返回按鈕返回到SideBarViewControllerSWRevealViewController中的後退按鈕

這是我如何設置optionAViewController。這工作。

//SideBarViewController 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
if ([segue isKindOfClass: [SWRevealViewControllerSegue class]]) { 
    SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue; 

    swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) { 

     UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController; 
     //[navController pushViewController:dvc animated: NO ]; 
     [navController setViewControllers: @[dvc] animated: NO ]; 
     [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES]; 
    }; 
} 

}

而且這是我想實現在OptionAViewController後退按鈕。我嘗試了popViewController但它崩潰。我也嘗試了其他選擇,但是他們都沒有工作。你知道選擇者應該是什麼?

//back button in ViewDidLoad (OptionAViewController) 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(popViewControllerAnimated:)forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
self.navigationItem.leftBarButtonItem = barButtonItem; 

回答

2

檢查此reference link爲更好地瞭解SWRevealViewController。

這將帶回您的側面菜單。

[_menuButton addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside]; 

不要忘記

#import "SWRevealViewController.h" 

在您的視圖控制器。

+0

這將回到側面菜單,但以當前視圖(OptionAViewController)作爲後視圖。我想實現一個返回按鈕返回到:SideBarViewController在前面和MainViewController在後面,當我第一次按下我的側邊欄圖標時它也會出現。 – nabrugir

1

試試這個代碼:

SWRevealViewController *reveal = self.revealViewController; 
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStyleBordered target:reveal action:@selector(revealToggle:)]; 

self.navigationItem.leftBarButtonItem = barBtn; 
1

試試這個代碼:

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbtn.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(goBack:)]; 

而在@selector方法:

-(void)goBack{ 
[self.navigationController popViewControllerAnimated:YES]; 
} 

希望這個作品。