传递给另一个类时 NSDictionary 设置为 nil (IOS)
当我从表视图转换到普通视图以显示详细信息时,我将 NSDictionary 对象从一个视图类传递到另一个视图类以显示详细信息:
Passing Controller:
[tweetController setTweet:tweet];
Receiving Controller.h:
@interface TweetViewController : UIViewController {
NSDictionary *tweet;
}
@property (nonatomic, retain) NSDictionary *tweet;
Receiving Controller.m:
@implementation TweetViewController
@synthesize tweet = _tweet;
然后我尝试使用此信息来设置在我看来,某些字段的属性:
- (void)viewDidLoad
{
[super viewDidLoad];
tweetLabel.text = [_tweet objectForKey:@"text"];
}
结果是一个空白标签,如果我在这个阶段检查 _tweet 的值,它是零。
我最初有一个方法可以设置 tweet 的值,我在与现在设置值相同的位置调用该方法。如果我在这个阶段检查这个值,那就没问题了。
我认为通过 @synthasize 的自动设置器正在工作,但在其他地方该值正在丢失。
抱歉,这是我的第一个 Objective C 东西!感谢您提前提供的任何帮助。
I am passing an NSDictionary object from one view class to another as I transition from a table view to a normal view to show details:
Passing Controller:
[tweetController setTweet:tweet];
Receiving Controller.h:
@interface TweetViewController : UIViewController {
NSDictionary *tweet;
}
@property (nonatomic, retain) NSDictionary *tweet;
Receiving Controller.m:
@implementation TweetViewController
@synthesize tweet = _tweet;
I then try to use this information to set the properties of some fields in my view:
- (void)viewDidLoad
{
[super viewDidLoad];
tweetLabel.text = [_tweet objectForKey:@"text"];
}
The result is a blank label and if I inspect the value of _tweet at this stage it is nil.
I originally had a method which set the value of tweet which I called at the same location as I am now setting the value. If I inspected the value at this stage it was fine.
I presume that the automagic setter through @synthasize is working, but somewhere else the value is being lost.
Sorry this is my first objective C anything! Thanks for any help in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在使用“tweet”实例变量,而“tweet”属性被合成为“_tweet”变量。
You are using your "tweet" instance variable, whereas the "tweet" property is synthesized to the "_tweet" variable.
您可能在 viewDidLoad 执行之后调用 setTweet 方法。
我通常将这种事情传递给自定义的 init 方法。
或者,您可以在将详细信息 VC 推送到导航堆栈之前进行设置。
You are probably calling the setTweet method after viewDidLoad executes.
I usually pass this kind of thing into a custom init method.
Alternatively, you could do the set before pushing the detail VC onto the nav stack.
您确定 tweetLabel 不为零吗?
我做了一些更正&对您的代码进行优化。您不再需要在头文件中声明 ivars,它们由
@synthesize
自动生成- (void)dealloc;
仅当您不使用时才需要弧。注意:
strong
相当于retain
Are you sure that tweetLabel isn't nil?
I've made a few corrections & optimisations to your code. You don't need to declare ivars in the header file anymore, they are generated automatically by
@synthesize
- (void)dealloc;
is only needed if you're not using ARC.Note:
strong
is equivalent toretain
为了扩展@Rayfleck的答案,由于您是 Objective-C 的新手,您的自定义 init 方法可能如下所示:
In TweetViewController.h:
In TweetViewController.m:
然后在您传递的控制器中,您将像这样分配和初始化:
To expand on @Rayfleck's answer, since you are new to Objective-C, your custom init method could look like this:
In TweetViewController.h:
In TweetViewController.m:
and then in your passing controller you'd allocate and initialize like this: