我的 FetchVenuesView
在 VenuesIDController
之前. VenuesIDController
是 tabbarcontroller
中的第二个选项卡项. FetchVenuesView
不是标签栏的一部分。
标签栏中的第一项是 tableview
我可以毫无问题地调用委托(delegate)。
但是,当我尝试在 VenuesIDController
中调用委托(delegate)时它总是在日志中显示为空。
我在这里做什么?我是否连接 Storyboard中的委托(delegate)?如何?
我有一个 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;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
VenueIDController *vid = [[VenueIDController alloc] init];
vid.delegate = self;
NSLog(@"%@",vid);
}
return self;
}
-(void) displayIDData:(NSDictionary*)data{
[delegate getID];
NSDictionary* idJSON = data;
}
最佳答案
您的 init
在 VenueIDController
出现错误。你已经在一个初始化中,所以你不需要创建另一个。相反,您应该拥有 self.delegate = self
. vid
您在那里创建的对象不会被保留。
关于objective-c - obj-c 委托(delegate)方法为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996804/