gpt4 book ai didi

objective-c - 如何在 Objective-C 中编写委托(delegate)模式

转载 作者:行者123 更新时间:2023-11-29 11:07:33 25 4
gpt4 key购买 nike

我正在尝试通过实现委托(delegate)/协议(protocol)将 JSON 传递给 VenueIDController。但是它似乎不起作用。请让我知道我错过了什么。我什至在 fetch 方法中输入了一个日志,看看它是否被调用,但它甚至没有记录,这意味着它没有被调用,请帮助。

我有一个 FetchVenuesViewController.h

#import "VenueTableViewController.h" 
#import "VenueIDController.h"

@interface FetchVenuesViewController : UIViewController< VenueTableViewControllerDelegate, VenueIDControllerDelegate>{
NSDictionary* venueJSON;
NSDictionary* idJSON;

};

@property (strong) NSDictionary* idJSON;


- (void)VenueFetch;

- (void)IDFetch;


@end

FetchVenuesViewController.m

@synthesize idJSON;

- (void)IDFetch {

//request some webservice


NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
//save the response



if (data) {

id IDJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (!IDJSON) {
//handle error

}
else {


//do something

}



} else {
// fetch failed

}

activityIndicator.hidden = NO;
}

-(NSDictionary *)getID{
[self IDfetch];
NSLog(@"json%@",idJSON);
return idJSON;
}

VenueIDController.h

@protocol VenueIDControllerDelegate;

@interface VenueIDController : UIViewController{

}


@property (assign) id <VenueIDControllerDelegate> delegate;
-(IBAction)getIDData:(id)sender;

@end

@protocol VenueIDControllerDelegate <NSObject>
-(NSDictionary *)getID;
@end

并在 VenueIDController.m

@interface VenueIDController (){
NSMutableArray* IDData;
UIImage* IDBarcode;
}
-(void) displayIDData:(NSDictionary*)data;
@end

@implementation VenueIDController
@synthesize delegate;


-(void) displayIDData:(NSDictionary*)data{

[delegate getID];


NSDictionary* idJSON = data;


}

最佳答案

这似乎是错误的:

-(void) displayIDData:(NSDictionary*)data{

[delegate getID];


NSDictionary* idJSON = data;


}

您正在调用委托(delegate)方法并丢弃其结果。

关于objective-c - 如何在 Objective-C 中编写委托(delegate)模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com