NSString 值不保留

发布于 2024-11-30 15:27:58 字数 80 浏览 1 评论 0原文

我正在尝试将字符串中的数据分配给不同视图控制器中的另一个字符串,但是似乎数据没有保留 - 我在 NSLog 中得到空响应。我想知道为什么,谢谢。。

I'm trying to assign data from a string to another string within a different viewcontroller however it seems that the data is not retained - i get a null response in NSLog. I would like to know why, thanks ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

桃扇骨 2024-12-07 15:27:58

尝试稍微改变一下顺序,如下所示,并使用 retain 而不是 copy:

SchoolDetailViewController *schoolController = [[SchoolDetailViewController alloc]initWithNibName:nil bundle:nil];
schoolController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
schoolController.courseDetails = @"passing new content"; 
[self presentModalViewController:schoolController animated:YES];
NSLog(@" %@",schoolController.courseDetails); // 'passing new content' is shown

.h
    NSString *courseDetails;
@property (nonatomic, retain) NSString *courseDetails; 

.m
@synthesize courseDetails; 

- (void)viewDidLoad {

    NSLog(@" text : %@",courseDetails); // returns null ... why? 

    [super viewDidLoad];
}

这应该可行。

Try changing the order a bit, as below, and use retain instead of copy:

SchoolDetailViewController *schoolController = [[SchoolDetailViewController alloc]initWithNibName:nil bundle:nil];
schoolController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
schoolController.courseDetails = @"passing new content"; 
[self presentModalViewController:schoolController animated:YES];
NSLog(@" %@",schoolController.courseDetails); // 'passing new content' is shown

.h
    NSString *courseDetails;
@property (nonatomic, retain) NSString *courseDetails; 

.m
@synthesize courseDetails; 

- (void)viewDidLoad {

    NSLog(@" text : %@",courseDetails); // returns null ... why? 

    [super viewDidLoad];
}

This should work.

无法回应 2024-12-07 15:27:58

这是因为当您呈现带有或不带有动画的视图控制器时,会调用 viewDidLoad 方法。

翻转这两个语句

[self presentModalViewController:schoolController animated:YES];
schoolController.courseDetails = @"passing new content"; 

所以只需像这样

schoolController.courseDetails = @"passing new content"; 
[self presentModalViewController:schoolController animated:YES];

然后再次检查结果......

Well that is because the viewDidLoad method is called when you present the view controller with or without animation.

So just flip these 2 statements

[self presentModalViewController:schoolController animated:YES];
schoolController.courseDetails = @"passing new content"; 

like this

schoolController.courseDetails = @"passing new content"; 
[self presentModalViewController:schoolController animated:YES];

And then check the results once again...

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