在视图之间传递图像

时间:2013-10-27 13:32:20

标签: 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, 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]

    }
}

文字每次出现都没有问题,但是没有详细的图片ViewController.Thanks求助。

detailViewController的viewDidLoad

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

1 个答案:

答案 0 :(得分:2)

首先,trickPhoto课程中的Trick应该是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];
}

更好的方法是在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];