2012-03-08 92 views
1

我有一個UITableViewController與圖像名稱列表。單擊名稱時,圖像的URL將在ImageViewController中設置,然後UITableViewController繼續變爲ImageViewController,其中包含UIImageView(與插座連接)。在ImageViewControllerviewDidLoad中,我將UIImageView設置爲來自URL的圖像,但是當我在模擬器上運行它時,圖像應該只出現黑屏。UIImageView不顯示從UITableViewController設置圖像

的SEGUE的一個小的視覺描述(其中 - >裝置賽格瑞):

  • 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 

對發生了什麼事有什麼想法? 謝謝!

回答

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

非常感謝ElJay,當我回家時我會試試這個。爲了將來的參考,什麼時候使用代表是適當的時間?我正在關注斯坦福大學的系列講座,以及我能夠告訴他們以這種方式使用的講座。 – gg67 2012-03-09 02:58:37

+1

代表是一種很好的方法,可以告訴班級您已完成某項工作。它們可以用來讓調用類知道某些事情已經完成,並且可以傳回對象(例如,當您記錄用戶並需要傳回UserInfo時)。他們沒有真正使用(從我的經驗)來呼籲某事。斯坦福的視頻很棒。 – 2012-03-09 03:08:39

+1

謝謝。好吧,試過後,我仍然只是黑屏。此外,延續到圖像需要更長的時間。思考? – gg67 2012-03-09 03:21:13

0

嘗試調用此方法:

[self.imageView setImage:self.image]; 

-

(void)imageListTableViewController:(ImageListTableViewController *)sender 
          withURL:(NSURL *)imageURL; 
+0

沒有運氣。雖然謝謝! – gg67 2012-03-09 03:04:32