UIImageView不显示来自UITableViewController的设置图像

时间:2012-03-08 23:37:58

标签: ios uiimageview imageview uitableview

我有UITableViewController,其中包含图片名称列表。点击名称后,图片的网址会在ImageViewController中设置,然后UITableViewController会转到包含ImageViewController的{​​{1}}(与插座连接) )。在UIImageView的{​​{1}}中,我将ImageViewController设置为来自网址的图片,但是当我在模拟器上运行时,图片应该只显示黑屏。

对segue的一点视觉描述(其中 - >表示segue):

  • viewDidLoad - > ImageViewController(有UIImageView

ImageListTableViewController

UIImageView

ImageListTableViewController.m

// The delegate here is the ImageViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Show Image"]) {
        [self setDelegate:segue.destinationViewController];
    }
}
// getting URL and calling the protocol method (in ImageViewController) which sets
// the image as an @property
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
    NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];
    NSLog([imageURL description]);
    [self.delegate imageListTableViewController:self withURL:imageURL];
}

有关正在发生的事情的任何想法? 谢谢!

2 个答案:

答案 0 :(得分:3)

不是使用委托的正确方法。试试这个: 在ImageViewController.h

@property(nonatomic,strong) NSURL *imageURL;

ImageViewController.m

@synthesize imageURL;

删除imageListTableViewController

更改viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    self.image = [UIImage imageWithData:imageData];
}

添加到viewDidUnload

[self setImageURL:nil];

然后在ImageListTableViewController.m中: 更改prepareForSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Show Image"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDictionary *imageDict = [self.imageArray objectAtIndex:indexPath.row];
        NSURL *imageURL = [FlickrFetcher urlForPhoto:imageDict format:FlickrPhotoFormatLarge];

        // Set the property on our Dest VC
        ImageViewController *ivc = segue.destinationViewController;
        ivc.imageURL = imageURL;

    }
}

更改您的didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"Show Image" sender:nil];
}

确保已将Segue连接到VC本身而不是TableView。如果不这样,Segue会在didSelectRowAtIndexPath被解雇之前被解雇。在这种情况下,它不会有所作为,但对于真正的控制 - 养成连接VC并在需要时调用performSegue的习惯。

答案 1 :(得分:0)

尝试调用此方法:

[self.imageView setImage:self.image];
< - > -

(void)imageListTableViewController:(ImageListTableViewController *)sender 
                             withURL:(NSURL *)imageURL;
相关问题