如何使用协议 [英] How to use protocol

查看:116
本文介绍了如何使用协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个viewControllers

I have two viewControllers


  • ServiceProvider

  • WorkLocation

ServiceProvider 我有一个文本字段和一个选择按钮。
当用户点击选择按钮时,它将重定向到 WorkLocation ,其中包含 location 的列表。
当用户选择任何位置然后点击 WorkLocation 中的选择按钮时,它将重定向回 ServiceProvider 包含 locationName Id 的页面。并在 ServiceProvider 页面 locationName 将显示在已存在的文本字段中。

In ServiceProvider I have one text field and a pick button. When user click on pick button it will redirect to WorkLocation which has a list of locations. When user select any location and then click on pick button from WorkLocation it will redirect back to ServiceProvider page with locationName and Id. And in ServiceProvider page locationName will be displayed in the text field which is already there.

要执行此操作,我使用的是协议。

To perform this I am using a protocol.

工作位置声明:

 #import <UIKit/UIKit.h>
 #import "WorkDetailCell.h"

@protocol WorkLocationDetailViewDelegate <NSObject>
@required
    -(void)organizationInfo:(NSString *) organization_id name:(NSString *)name;
@end

@interface WorkLocationDetailsViewController :UIViewController < UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate >
{
    IBOutlet UITableView *workLocationTableView;
    IBOutlet UISearchBar *workLocationSearchBar;
    NSMutableArray *workDetailArray,*tempDataArray,*searchArray,*searchResultArray;
    BOOL search;
 }

 @property (nonatomic,retain) id < WorkLocationDetailViewDelegate > delegate;

WorkLocation实施:

WorkLocation implementation:

 -(IBAction)doneButtonClicked:(id)sender {
    if (search==FALSE) {
        NSLog(@"pick%d",[sender tag]);
        NSLog(@"pick from temp");
         NSDictionary *detailList=[tempDataArray objectAtIndex:[sender tag]];
        [_delegate organizationInfo:detailList[@"organizationId"] name:detailList[@"organizationName"]];
    } else {
        NSLog(@"pick from search");
    }

    [self.navigationController popToRootViewControllerAnimated:YES];
}

和ServiceProvider实施文件:

and in ServiceProvider implementation file:

-(void)organizationInfo:(NSString*)organization_id name:(NSString *)name {
    NSString *id=organization_id;
    txtWorkLocation.text=name;
    NSLog(@"%@ %@",organization_id,name);
}

现在当我从 WorkLocation回来时 page此方法未被调用。 的ServiceProvider
声明:

Now when I came back from WorkLocation page this method is not called. Declaration of ServiceProvider:

#import "WorkLocationDetailsViewController.h"

@class RadioButtonView; 
@class WorkLocationDetailsViewController;

@protocol ServiceProviderProfileDelegate <NSObject>
@required
    -(void)setProImages:(UIImage *)img;
@end

@interface ServiceProviderProfileViewController : UIViewController < UINavigationControllerDelegate, UIImagePickerControllerDelegate, RadioButtonDelegate, WorkLocationDetailViewDelegate >
{
    NSString *gratuityStr,*paymentStr;
    RadioButtonView *gratuityGroup,*paymentGroup;
    IBOutlet UILabel *usernameLabel;
    NSString *gratuitySelection,*receivePayment;
    WorkLocationDetailsViewController *WorkVC;
}


推荐答案

你为什么不用prepareForSegue:设置目标视图控制器的文本字段的方法?这将以标准方式在视图控制器之间进行通信,并且还可以减少编码工作量。

Why don't you use prepareForSegue: method to set text field of the destination view controller? This will make the communication between the view controllers in a standard way, and also reduce your coding effort.

无论如何,实现协议的代码看起来还不错。您需要检查WorkVC是否在其委托设置之前启动。

Anyways, your code for implementing protocol looks ok. You need to check WorkVC is initiated before its delegate is set.

这篇关于如何使用协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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