gpt4 book ai didi

ios - 即使在 ios 中进行导航时分配后数据也为零, objective-c

转载 作者:行者123 更新时间:2023-11-28 21:27:02 25 4
gpt4 key购买 nike

我正在尝试将文本值、图像和 uitextview 从一个 View Controller 传递到另一个 View Controller 。但即使在完成赋值后,这些值仍为 nil。

赋值语句执行后titellabel、imageView、descriptionView为null。我已经分配并初始化了 DetailsViewController 但在分配值后这些仍然显示为 nil。你能告诉我我在这里缺少什么吗?为什么他们不接受分配的值。

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
//DetailsViewController *view = [[DetailsViewController alloc]init];
DetailsViewController *view = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
NSLog([[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"title"]);
view.titleLabel.text = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"title"];
view.imageView.image = [self.images objectAtIndex:indexPath.row];

view.descriptionView.text = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"description"];
[self.navigationController pushViewController:view animated:TRUE];
}

最佳答案

纳文拉姆,

您不能在加载 viewComponment 之前设置它们的属性。

view.titleLabel.text = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"title"];
view.imageView.image = [self.images objectAtIndex:indexPath.row];
view.descriptionView.text = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"description"];

以上三个语句将设置尚未加载的textLabel、textView和UIImageView的属性,因此它永远不会显示。

你可以做什么:

DetailsViewController.h

中声明三个属性,第一个用于保存字符串,第二个用于保存图像,第三个用于保存 descriptionText

我们称它们为

@property (nonatomic,strong) NSString *title;
@property (nonatomic,strong) UIImage *passedImage;
@property (nonatomic,strong) NSString *descriptionText;

将索引路径处的选择行修改为

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
//DetailsViewController *view = [[DetailsViewController alloc]init];
DetailsViewController *view = [[DetailsViewController alloc] initWithNibName:@"DetailsViewController" bundle:nil];
NSLog([[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"title"]);
view.title = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"title"];
view.passedImage = [self.images objectAtIndex:indexPath.row];
view.descriptionText = [[self.xmlNews objectAtIndex:indexPath.row] objectForKey:@"description"];
[self.navigationController pushViewController:view animated:TRUE];
}

最后在 DetailsViewController 的 ViewDidLoad 中将图像和标签设置为

self.titleLabel.text = self.title;
self.imageView.image = self.passedImage;
self.descriptionView.text = self.descriptionText;

希望对你有帮助

关于ios - 即使在 ios 中进行导航时分配后数据也为零, objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com