Jozef Vrana
Jozef Vrana

Reputation: 309

Passing image between views

I am solving this issue about 2 weeks and only be able to pass string, not image.

I store the image from camera or from library in this method.

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

In tableViewClass I store the values into properties of 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]

    }
}

Text showed up every time without problems, but there is no image in detailViewController.Thanks for help.

viewDidLoad of detailViewController

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

Upvotes: 1

Views: 123

Answers (1)

iwasrobbed
iwasrobbed

Reputation: 46703

First off, trickPhoto within the Trick class should be a UIImage, not a UIImageView. Models shouldn't know anything about views such as frames, etc so right now you're violating the MVC design pattern.

Then it'd be:

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

The better way of doing this would be to create a Trick property within DetailViewController and just pass the whole trick into the 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;
    }
}

Then you'd just populate it with:

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

Upvotes: 2

Related Questions