Ios 在视图之间传递图像

Ios 在视图之间传递图像,ios,objective-c,uiimageview,uitableview,Ios,Objective C,Uiimageview,Uitableview,我解决这个问题大约2周,只能传递字符串,而不能传递图像 我用这种方法存储相机或库中的图像 -(IBAction)saveAction:(id)sender { Tricks *trick = [[Tricks alloc]init]; trick.trickName = self.trickLabel.text; trick.trickPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 356, 320, 3

我解决这个问题大约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
。模型不应该知道任何有关视图(如框架)的信息,所以现在您违反了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
属性,然后将整个技巧传递给视图控制器

- (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];

要理解你想做什么有点困难:技巧课是做什么的?是单身吗?看起来您正在将技巧类的实例添加到从类方法返回的数组中,该数组。。。odd.static NSMutableArray*trickList=nil+(NSMutableArray*)trickList{if(!trickList){trickList=[[NSMutableArray alloc]init];}返回trickList;}我正在向tableView添加一个单元格,这代表了一个技巧。在detailView中有一个名称和一张图片,但您的行[[Tricks Drillist]addObject:trick];返回和数组,其中包含技巧对象,但由于没有人引用它,因此一旦退出该方法(紧跟在该行之后),数组将被释放。我假设你在使用ARC…是的,我在使用ARC…那么你建议我如何传递信息?文本传递没有问题SiwasRobbed在下面给你一个非常好的例子,说明什么是Objective-C中最常用的模式之一…你可以想象我现在有多感激…你真的帮了我很多。以前的问题是,我使用UIImageView而不是UIImage@JozefVrana很乐意帮忙!如果这回答了您的问题,请按我答案旁边的复选标记结束问题。