我有一个 UITableViewController
带有图像名称列表。单击名称时,图像的 URL 设置在 ImageViewController
中。然后是 UITableViewController
转至 ImageViewController
包含 UIImageView
(与 socket 相连)。在 ImageViewController
的viewDidLoad
我设置了UIImageView
从 URL 到图像,但是当我在模拟器上运行它时,只有一个黑屏出现在图像应该出现的位置。
对转场的一点视觉描述(其中 -> 表示转场):
ImageListTableViewController
–> ImageViewController(有 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];
}
ImageViewController.m
#import "ImageViewController.h"
@implementation ImageViewController
@synthesize image = _image;
@synthesize imageView = _imageView;
- (void)imageListTableViewController:(ImageListTableViewController *)sender
withURL:(NSURL *)imageURL;
{
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
self.image = [UIImage imageWithData:imageData];
}
#pragma mark - View lifecycle
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
// nothing
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.imageView setImage:self.image];
}
- (void)viewDidUnload
{
[self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
关于发生了什么的任何想法?
谢谢!
最佳答案
不是真正使用委托(delegate)的正确方法。试试这个:
在 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 的习惯。
关于ios - UIImageView 不显示来自 UITableViewController 的设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9626802/