1

我正在解决这个问题大约 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];
4

1 回答 1

2

首先,类trickPhotoTrick应该是 a UIImage,而不是 a 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];
}

更好的方法是在其中创建一个Trick属性DetailViewController并将整个技巧传递给视图控制器。

- (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];
于 2013-10-27T13:50:45.977 回答