首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ios,目标c中进行导航时,即使在分配之后,数据也为零。

问在ios,目标c中进行导航时,即使在分配之后,数据也为零。
EN

Stack Overflow用户
提问于 2016-06-07 08:10:54
回答 1查看 50关注 0票数 0

我试图将文本值、图像和uitextview从一个视图控制器传递到另一个视图控制器。但即使在完成任务后,这些值仍为零。

执行赋值语句后,titellabel、imageView和descriptionView为空。我已经分配并初始化了DetailsViewController,但在赋值后,这些值仍然显示为零。你能说出我在这里错过了什么吗?为什么他们不接受所分配的值。

代码语言:javascript
复制
- (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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-07 08:16:54

纳文·RAM

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

代码语言:javascript
复制
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中保存DetailsViewController.h

让我们称他们为

代码语言:javascript
复制
@property (nonatomic,strong) NSString *title;
@property (nonatomic,strong) UIImage *passedImage;
@property (nonatomic,strong) NSString *descriptionText;

将索引路径上的行修改为

代码语言:javascript
复制
- (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];
}

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

代码语言:javascript
复制
self.titleLabel.text = self.title;
self.imageView.image = self.passedImage;
self.descriptionView.text = self.descriptionText;

希望它能帮上忙

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37673892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档