创建代理以从菜单更改视图 [英] Creating a delegate to change views from a menu

查看:131
本文介绍了创建代理以从菜单更改视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够从我的自定义菜单推送视图控制器到我的主要子类导航控制器,我想使用委托。在我试图更改视图控制器之前,我去了一些更容易,更改当前视图控制器标题。
这是我写的代码,但是在TableView单元格上的录制不会触发委托。

  - > RootViewController 
/ \\ \\
容器容器
| |
SlideMenuViewController SubClassed UINavigationController
| | | | |
UITableViewController VC1 VC2 VC3 VC4


SlideMenuViewController.h:

  @protocol SlidingMenuDelegate< NSObject> 

@end

@interface SlideMenuViewController:UIViewController
{
id< SlidingMenuDelegate>代表;
}
@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: b
$ b

  @interface MyNavigationController:UINavigationController< UINavigationControllerDelegate> 

@property(nonatomic,strong)UILabel * titleForView;

MyNavigationController.m

   - (void)showRightMenu 
{
...
...
//一些动画滑动菜单out

//委托的东西
//获取storyboard的实例。
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@MainStoryboardbundle:[NSBundle mainBundle]];

SlideMenuViewController * slideMenuVC;

//从storyboard的实例获取viewcontrollers实例
slideMenuVC = [storyBoard instantiateViewControllerWithIdentifier:@slideMenuSID];
slideMenuVC.delegate = self;
}


解决方案

您的 SlidingMenuDelegate 协议需要一些(可选)方法,例如 slidingMenu:didSelectOption:



然后在 tableView:didSelectRowAtIndexPath:

code>方法,你会做

  if([self.delegate responsesToSelector:@selector(slidingMenu:didSelectOption :) ]){
SlidingMenuOption选项;

switch(indexPath.row){
case 0:
option = SlidingMenuFooOption;
break;

case 1:
option = SlidingMenuBarOption;
break;

// ...
}

[self.delegate slidingMenu:self didSelectOption:option];
}

最后,您的委托会响应 slidingMenu:didSelectOption:消息(例如设置导航控制器的标题)。


I want to be able to push view controllers to my main subclassed navigation controller from my custom menu and I want to do it using a delegate. Before I'm trying to change view controllers I went for something easier , to change the current view controller title. This is the code I wrote so far but taping on the TableView cell will not trigger the delegate. (or so it seems)

Please advise

                -> RootViewController
                 /                   \
            Container                Container       
                |                         |
     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;
}

解决方案

Your SlidingMenuDelegate protocol needs some (optional) methods such as slidingMenu:didSelectOption:. Also, I would define an enum or something that specifies all of the options that the menu provides.

Then in your tableView:didSelectRowAtIndexPath: method, you would do

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];
}

Finally, your delegate would do something in response to this slidingMenu:didSelectOption: message (e.g. set the title of the navigation controller).

这篇关于创建代理以从菜单更改视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆