简体   繁体   English

在委托中初始化自定义对象

[英]Initializing custom objects inside delegate

I've created a text field using storyboard and am trying to setup the delegates. 我使用情节提要创建了一个文本字段,并尝试设置委托。 The process looks straightforward and the callback is called when I expect, but my custom object, which I attempt to initialize in the callback, doesn't seem to initialize. 该过程看起来很简单,并且在需要时调用了回调,但是我试图在回调中初始化的自定义对象似乎没有初始化。 I believe the relevant bits of code are in the ViewController for the view that contains the text field and in the custom object. 我相信相关的代码位在包含文本字段的视图的ViewController中和自定义对象中。

Oddly, in the debugger, I see a property at self.feed that has the right methods, but I can't seem to log from within its initializer, which has me quite confused. 奇怪的是,在调试器中,我在self.feed上看到了一个属性,该属性具有正确的方法,但似乎无法从其初始化程序中登录,这让我感到非常困惑。

Here's the callback for the text field: 这是文本字段的回调:

-(BOOL) textFieldShouldReturn: (UITextField *)textField {    
    NSLog(@"Begin feeding."); // This logs

    NSURL *path = [NSURL URLWithString: @"http://feeds2.feedburner.com/hackaday/LgoM"];
    self.feed = [[Feed alloc] initWithContentsOfURL: path];

    NSLog(@"Should have a feed."); // So, does this.
    [textField resignFirstResponder];

    return YES;
}

Here's the Feed interface: 这是Feed界面:

#import <Foundation/Foundation.h>
#import "FeedItem.h"
#import "FeedParser.h"
#import "FeedParserDelegate.h"

@interface Feed : NSMutableArray <NSURLConnectionDelegate, FeedParserDelegate>{
    BOOL isValidFeed;
    FeedParser *parser;
    NSMutableData *xmlData;
    NSURLConnection *connection;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSURL *link;
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) NSString *description;

- (id) initWithUrl: (NSURL *) url; 
- (BOOL) isValidFeed;

- (void) _populate;
- (void) _fetchData;

@end

And finally the initializer I'm hoping will be called, but doesn't seem to be: 最后,我希望的初始化程序将被调用,但似乎不是:

#import "Feed.h"

@implementation Feed

@synthesize name;
@synthesize link;
@synthesize url;
@synthesize description;


- (id) initWithUrl: (NSURL *) feedUrl{
    NSLog(@"initing with NSUrl"); // This never logs.
    self = [super init];

    if (self) {
        self.url = feedUrl;
        [self _populate];
    }

    return self;
}

Essentially, my desired outcome is to get initing with NSUrl to log to the console. 本质上,我想要的结果是initing with NSUrl以登录到控制台。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Please, let me know if there are other relevant details I should post (I'm obviously new to iOS). 请让我知道我是否应该发布其他相关详细信息(我显然是iOS新手)。

You're calling initWithContentsOfURL: , but you've implemented initWithUrl: . 您正在调用initWithContentsOfURL:但是您已经实现了initWithUrl: Aren't you getting a compiler warning? 您没有收到编译器警告吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM