传递给另一个类时 NSDictionary 设置为 nil (IOS)

发布于 2024-12-29 18:21:32 字数 850 浏览 0 评论 0原文

当我从表视图转换到普通视图以显示详细信息时,我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毅然前行 2025-01-05 18:21:32

您正在使用“tweet”实例变量,而“tweet”属性被合成为“_tweet”变量。

You are using your "tweet" instance variable, whereas the "tweet" property is synthesized to the "_tweet" variable.

难得心□动 2025-01-05 18:21:32

您可能在 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.

丑丑阿 2025-01-05 18:21:32

您确定 tweetLabel 不为零吗?

我做了一些更正&对您的代码进行优化。您不再需要在头文件中声明 ivars,它们由 @synthesize 自动生成

- (void)dealloc; 仅当您不使用时才需要弧。

//.h
@interface TweetViewController : UIViewController


@property (strong, nonatomic) NSDictionary *tweet;
@property (strong, nonatomic) IBOutlet UILabel *tweetLabel

@end


//.m
@implementation TweetViewController

@synthesize tweet = _tweet;
@synthesize tweetLabel = _tweetLabel;


- (void)viewDidLoad {
    [super viewDidLoad];

    self.tweetLabel.text = [self.tweet objectForKey:@"text"];
}

- (void)dealloc {

    [_tweet release];
    [_tweetLabel release];

    [super dealloc];
}

@end

注意: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.

//.h
@interface TweetViewController : UIViewController


@property (strong, nonatomic) NSDictionary *tweet;
@property (strong, nonatomic) IBOutlet UILabel *tweetLabel

@end


//.m
@implementation TweetViewController

@synthesize tweet = _tweet;
@synthesize tweetLabel = _tweetLabel;


- (void)viewDidLoad {
    [super viewDidLoad];

    self.tweetLabel.text = [self.tweet objectForKey:@"text"];
}

- (void)dealloc {

    [_tweet release];
    [_tweetLabel release];

    [super dealloc];
}

@end

Note: strong is equivalent to retain

中二柚 2025-01-05 18:21:32

为了扩展@Rayfleck的答案,由于您是 Objective-C 的新手,您的自定义 init 方法可能如下所示:

In TweetViewController.h:

- (id)initWithTweet:(NSDictionary*)tweet;

In TweetViewController.m:

- (id)initWithTweet:(NSDictionary*)tweet
{
    self = [super init];
    if (self) {
       _tweet = tweet;
    }
    return self;
}

然后在您传递的控制器中,您将像这样分配和初始化:

TweetViewController *tvc = [[TweetViewController alloc] initWithTweet:myTweet];

To expand on @Rayfleck's answer, since you are new to Objective-C, your custom init method could look like this:

In TweetViewController.h:

- (id)initWithTweet:(NSDictionary*)tweet;

In TweetViewController.m:

- (id)initWithTweet:(NSDictionary*)tweet
{
    self = [super init];
    if (self) {
       _tweet = tweet;
    }
    return self;
}

and then in your passing controller you'd allocate and initialize like this:

TweetViewController *tvc = [[TweetViewController alloc] initWithTweet:myTweet];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文