0

我希望能够从我的自定义菜单将视图控制器推送到我的主要子类导航控制器,并且我想使用委托来完成。在我尝试更改视图控制器之前,我尝试了一些更简单的方法,即更改当前视图控制器标题。这是我到目前为止编写的代码,但在 TableView 单元格上点击不会触发委托。(或者看起来)

请指教

                -> 根视图控制器
                 / \
            集装箱集装箱       
                | |
     SlideMenuViewController 子类 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";
}

我的导航控制器.h:

@interface MyNavigationController : UINavigationController <UINavigationControllerDelegate>

@property (nonatomic, strong) UILabel *titleForView;

我的导航控制器.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;
}
4

1 回答 1

0

您的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:消息(例如设置导航控制器的标题)。

于 2013-07-07T15:56:53.250 回答