在控制器之间传递数据

时间:2013-02-18 14:19:59

标签: objective-c uiimageview

我正在尝试将网址从UITableViewController转移到UIViewController,但由于某种原因,图片无法从提交的网址UIImageView中显示。这是我的代码:

TableView.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = @"http://www.macdigger.ru/wp-content/uploads/2013/02/Apple-Nokia-Samsung-1.jpg";

    ImageViewController *imageView = [[ImageViewController alloc] init];        
    [self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"imageViewController"] animated:YES];

    [imageView setDetailItem:url];

}

ImageViewController.m

@interface ImageViewController ()
- (void)configureView;
@end

@implementation ImageViewController

- (void)setDetailItem:(id)newUrl
{
    NSLog(@"%@", newUrl);
    self.urlOfImage = newUrl;
    [self configureView];

}

- (void)configureView
{
        NSURL *url = [NSURL URLWithString:_urlOfImage];
        NSLog(@"%@",url); //There URL is normally displayed in the log
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        _imageView.image = image;//And then the picture does not want to output
}

在日志中显示传递的URL(粗体显示)。事实证明,URL本身已通过,但UIImageView不会显示此网址中的图片。

P.P.S。 数据"<ffd8ffe1 00184578 69660000 49492a00 08000000 00000000 00000000 ffec0011 4475636b 79000100 04000000 460000ff e1031b68 7474703a 2f2f6e73 2e61646f 62652e63 6f6d2f78 61702f31 2e302f00 3c3f7870 61636b65 74206265 67696e3d 22efbbbf 22206964...>"

的形式显示在日志中

2 个答案:

答案 0 :(得分:1)

可能是错误,因为尚未加载ViewController,尝试检查它是否已加载setDetailItem方法,如果未加载 - 在viewDidLoad中配置configure:

- (void)setDetailItem:(id)newUrl
{
    NSLog(@"%@", newUrl);
    self.urlOfImage = newUrl;
    if ([self isViewLoaded]) {
        [self configureView];
    }

}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    if (self.urlOfImage) {
        [self configureView]
    }
}

答案 1 :(得分:1)

为什么不再制作另一个初始化程序?

ImageViewController *imageView = [[ImageViewController alloc] initWithURL:urlToGo];
<。>在.m文件中,顶部:

@interface ImageViewController ()
@property (nonatomic, strong) NSURL *url;
@end

- (id)initWithURL:(NSURL *url){
   self = /* any kind of normal initialization, xib or storyboard */ [super init];
   if (self) {
       _url = url;
   }
}

- (void)viewDidLoad {
   [super viewDidLoad];
   [self configureView];
}