控制器之间通过自定义代理进行通信

时间:2016-10-24 15:27:17

标签: ios objective-c delegates

我已从Dictionary获得服务器的回复。现在,我在ViewControllerA上有一个按钮。点击该按钮,我想显示dictionary tableViewCells ViewControllerBNotifications获取的详细信息。我已经通过delegates完成了这项任务,但我无法理解通过delegates如何完成同样的任务,并且需要通过#####Code for Network Class -(void)getResponse:(NSString *)url{ NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; [urlRequest setHTTPMethod:@"GET"]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { //check if we encountered an error if(error != nil){ NSLog(@"%@", [error localizedDescription]); }else{ //get and check the HTTP status code NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode]; if (HTTPStatusCode != 200) { NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode); } [[NSOperationQueue mainQueue] addOperationWithBlock:^{ if(data != nil){ NSError *parseError = nil; NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadNotification" object:self userInfo:responseDictionary]; NSLog(@"The response is - %@",responseDictionary); } }]; } }]; [task resume]; } 来实现。

ViewControllerClass

-(void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyReload:) name:@"ReloadNotification" object:nil]; } 有按钮:

delegates

请通过代码解释如何通过delegates完成任务?我处于开发的初始阶段......在阅读了几篇教程后,我们也无法理解stuff

1 个答案:

答案 0 :(得分:1)

我会帮助你

ViewControllerA.h

#import <UIKit/UIKit.h>
@class  ViewControllerA;
@protocol ViewControllerADelegate <NSObject>

- (void)viewControllerA:(ViewController *)viewControllerA showDictinaoryDataOnTableView:(NSDictionary *)dict;

@end

@interface ViewControllerA : UIViewController

@property (nonatomic, assign)id<ViewControllerDelegate> delegate;

- (IBAction)doneButtonTapped:(id)sender;
@end

ViewControllerA.m

#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA ()

@end

@implementation ViewControllerA



- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
}


- (IBAction)actionGoNext:(id)sender;
{
   [self.delegate viewControllerA:self showDictinaoryDataOnTableView:dict];  //Here pass your dict
   ViewControllerB *vcB = [ViewController  alloc] initWithNibName:@"ViewControllerB" bundle:nil;
   [self.navigationController pushViewController:vcB animated:YES];
}

ViewControllerB.h

#import <UIKit/UIKit.h>
#import "ViewControllerB.h" 
@interface ViewControllerB : UIViewController <ViewControllerADelegate>
@property (nonatomic, strong) IBOutlet UITableView *tblDictData;
@end

ViewController.B

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

@synthesize tblDictData;

- (void)viewDidLoad
{
  [super viewDidLoad];
}

- (void)viewControllerA:(ViewControllerA *)viewControllerA showDictinaoryDataOnTableView:(NSDictionary *)dict
{
    yourdict = dict;    
    ... Must Do your stuff for showing data from dictionary to table view here 
    //Then reload the table view
    [tblDictData reloadData];    
}