创build一个委托来从菜单中更改视图
我想能够从我的自定义菜单将视图控制器推到我的主子类导航控制器,我想要使用委托。 在我试图改变视图控制器之前,我去了一些更容易,改变当前的视图控制器标题。 这是迄今为止我写的代码,但在TableView单元上粘贴不会触发委托。 (或者看起来)
请指教
- > RootViewController / \ 容器容器 | | SlideMenuViewController SubClassed UINavigationController | | | | | UITableViewController VC1 VC2 VC3 VC4
SlideMenuViewController.h:
@protocol SlidingMenuDelegate <NSObject> @end @interface SlideMenuViewController : UIViewController { id <SlidingMenuDelegate> delegate; } @property (strong, nonatomic) id delegate;
SlideMenuViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //do something to send controller ((UIRTLNavigationController *)(self.delegate)).titleForView.text = @"test"; }
MyNavigationController.h:
@interface MyNavigationController : UINavigationController <UINavigationControllerDelegate> @property (nonatomic, strong) UILabel *titleForView;
MyNavigationController.m:
- (void)showRightMenu { ... ... //Some animation to slide the menu out //Delegate stuff //Get the storyboard's instance. UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]]; SlideMenuViewController *slideMenuVC; //Get the viewcontrollers instance from the storyboard's instance slideMenuVC = [storyBoard instantiateViewControllerWithIdentifier:@"slideMenuSID"]; slideMenuVC.delegate = self; }
您的SlidingMenuDelegate
协议需要一些(可选)方法,如slidingMenu:didSelectOption:
另外,我会定义一个枚举或者指定菜单提供的所有选项的东西。
然后在你的tableView:didSelectRowAtIndexPath:
方法中,你会这样做
if ([self.delegate respondsToSelector:@selector(slidingMenu:didSelectOption:)]) { SlidingMenuOption option; switch (indexPath.row) { case 0: option = SlidingMenuFooOption; break; case 1: option = SlidingMenuBarOption; break; // ... } [self.delegate slidingMenu:self didSelectOption:option]; }
最后,你的委托会做一些响应这个slidingMenu:didSelectOption:
消息(例如设置导航控制器的标题)。