Delegate example

Target: PolldownMenuViewController (popover) pass a value to TasksViewController.

1.  In PolldownMenuViewControllerDelegate.h file, create a protocol and its property.

     @protocol PolldownMenuViewControllerDelegate <NSObject>

     – (void)didSelectTypeFilter:(NSString*)filter;

     @end

     @property (nonatomic, unsafe_unretained) id<PolldownMenuViewControllerDelegate> delegate;

Then, trigger the delegate function to require popover pass it to TasksViewController

     – (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     BaseMenuTreeTableViewCell* cell = (BaseMenuTreeTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
     if (_delegate && [_delegate respondsToSelector:@selector(didSelectTypeFilter:)])
           [_delegate didSelectTypeFilter:cell.treeItem.base];

 

2.  In TasksViewController.m file, declare need PolldownMenu~Delegate and implement delegate function.

     @interface TasksViewController () <UIActionSheetDelegate, CVDataModelDelegate, UITableViewDelegate, UISearchBarDelegate, RowItemDelegate, PolldownMenuViewControllerDelegate>

     #pragma mark – 
     #pragma mark PolldownMenuViewControllerDelegate

     – (void)didSelectTypeFilter:(NSString*)filter {

          if ([filter isEqualToString:TYPE_OPTION_ALL_LS]) {
                [_model updateWithType:TASK_TYPE_ALL];
          } else if ([filter isEqualToString:TYPE_OPTION_MESSAGE_LS]) {
                [_model updateWithType:TASK_TYPE_MESSAGE];
          } else if ([filter isEqualToString:TYPE_OPTION_MAIL_LS]) {
                [_model updateWithType:TASK_TYPE_MAIL];
          } else if ([filter isEqualToString:TYPE_OPTION_DISCUSSION_LS]) {
                [_model updateWithType:TASK_TYPE_DISCUSSION];
          } else if ([filter isEqualToString:TYPE_OPTION_FYI_LS]) {
                [_model updateWithType:TASK_TYPE_FYI];
          } else if ([filter isEqualToString:TYPE_OPTION_CHAT_LS]) {
                [_model updateWithType:TASK_TYPE_CHAT];
          } else if ([filter isEqualToString:TYPE_OPTION_TODO_LS]) {
                [_model updateWithType:TASK_TYPE_TODO];
          } else if ([filter isEqualToString:TYPE_OPTION_ACTION_LS]) {
                [_model updateWithType:TASK_TYPE_ACTION];
          }else if ([filter isEqualToString:TYPE_OPTION_APPROVAL_LS]) {
                [_model updateWithType:TASK_TYPE_APPROVAL];
          } else {
                [_model updateWithType:TASK_TYPE_ALL];
          }
          [_typeButton setTitle:filter forState:UIControlStateNormal];
          [self showLoading:YES];

          [_popoverForSource dismissPopoverAnimated:YES];      //cause in property, there is a 

@property(nonatomic, retain) UIPopoverController* popoverForSource;, we could use it directly.

          return;
     }

3.   We also need to set a delegate for PolldownViewController side. Since we have never set a delegate for polldownViewController.

    – (void) typeItemTouched {

          _nav = [[PolldownMenuViewController alloc]initWithDelegate:self];

For PolldownMenuViewController.h, add: 

     – (id)initWithDelegate:(id<CVPolldownMenuViewControllerDelegate>)delegate;

For PolldownMenuViewController.m, add:

     – (id)initWithDelegate:(id<CVPolldownMenuViewControllerDelegate>)delegate {
          if (self = [super init])
                self.delegate = delegate;
     return self;
    }

Result:

 

 

Image

Image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s