objective-c - obj-c 委托(delegate)方法为空

标签 objective-c ios uiviewcontroller delegates storyboard

我的 FetchVenuesViewVenuesIDController 之前. VenuesIDControllertabbarcontroller 中的第二个选项卡项. 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;


    }

最佳答案

您的 initVenueIDController出现错误。你已经在一个初始化中,所以你不需要创建另一个。相反,您应该拥有 self.delegate = self . vid您在那里创建的对象不会被保留。

关于objective-c - obj-c 委托(delegate)方法为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996804/

相关文章:

swift - 在应用程序委托(delegate)中识别和加载 View Controller 时出现“使用无法解析的标识符 'rootViewController'”错误

objective-c - 使用 [key] 表示法访问 NSDictionary 中的键?

ios - 从 TimerCallback 实例调用时,调用 Dropbox API Client.Files.DownloadAsync 不会返回元数据

ios - 正则表达式在 iOS 上无法正常工作

iphone - 如何在不让原始 View 消失的情况下将 modalViewController 覆盖在另一个 View 之上?

ios - 如何以正确的方式实现委托(delegate)方法?

ios - 另一个 UIView 的 UIView subview 是 UIViewController 的 subview ,在 iOS 中没有以 UIViewController 为中心

ios - 无需重新初始化即可访问 View Controller

ios - 如何在 Objective-C 中从我的 IOS 应用程序进行 Whatsapp 语音通话

ios - UIViewController 抛出 'unrecognized selector' 错误