gpt4 book ai didi

ios - 在 View 之间传递图像

转载 作者:行者123 更新时间:2023-11-29 10:53:03 26 4
gpt4 key购买 nike

我解决这个问题大约 2 周,只能传递字符串,不能传递图像。

我用这种方法存储来自相机或库的图像。

-(IBAction)saveAction:(id)sender
{

Tricks *trick = [[Tricks alloc]init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 356, 320, 305)];
trick.trickPhoto.image = self.ImagePhoto.image;
[[Tricks trickList]addObject:trick];
}

在 tableViewClass 中,我将值存储到 detailView 的属性中

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"detailTrick"]){
NSIndexPath *indexPath = nil;

indexPath = [self.tableView indexPathForSelectedRow];

DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
detailViewController.trickPhoto = [[UIImageView alloc]initWithFrame:CGRectMake(0, 358, 200, 200)];
detailViewController.fileText = trick.trickName;
detailViewController.trickPhoto = trick.trickPhoto;
//object = [Tricks trickList][indexPath.row]

}
}

文本每次都显示没有问题,但在 detailViewController 中没有图像。感谢帮助。

detailViewController的viewDidLoad

    [super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:@"%@",_fileText] forState:UIControlStateNormal];
[self.trickPhoto setImage:_trickPhoto.image];

最佳答案

首先,Trick 类中的 trickPhoto 应该是 UIImage,而不是 UIImageView。模型不应该知道任何有关 View (例如框架等)的信息,所以现在您违反了 MVC 设计模式。

然后就是:

- (IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc] init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = self.ImagePhoto.image;
[[Tricks trickList] addObject:trick];
}

更好的方法是在 DetailViewController 中创建一个 Trick 属性,然后将整个 trick 传递到 View Controller 中。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"detailTrick"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
NSLog(@"Make sure trick isn't nil: %@", trick);
detailViewController.trick = trick;
}
}

然后你只需填充它:

[super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:@"%@", self.trick.trickName] forState:UIControlStateNormal];
[self.trickPhoto setImage:self.trick.trickPhoto];

关于ios - 在 View 之间传递图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19618621/

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