obj-c委托方法为null

时间:2012-10-21 09:54:40

标签: objective-c ios uiviewcontroller delegates storyboard

我的FetchVenuesView位于VenuesIDController之前。 VenuesIDControllertabbarcontroller中的第二个标签栏项。 FetchVenuesView不属于标签栏。

标签栏中的第一项是tableview,在其中我可以毫无问题地呼叫代表。 但是,当我尝试在VenuesIDController中调用委托时,它始终在日志中显示为null。

我在这做什么?我是否在故事板中连接了代理人?怎么样?

我有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;


    }

1 个答案:

答案 0 :(得分:0)

init VenueIDController出现错误。你已经在初始阶段,所以你不需要创建另一个。相反,你应该self.delegate = self。 vid对象不会被保留。