Objective c 在控制器之间传递数据

Objective c 在控制器之间传递数据,objective-c,uiimageview,Objective C,Uiimageview,我正在尝试将URL从UITableViewController传输到UIViewController,但由于某些原因,图像无法从提交的URL显示在UIImageView中。这是我的密码: TableView.m - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *url = @"http://www.macdigger.ru/wp-c

我正在尝试将URL从
UITableViewController
传输到
UIViewController
,但由于某些原因,图像无法从提交的URL显示在
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];

}
@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
}
ImageViewController.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];

}
@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
没有显示来自此URL的图像


p.p.S.数据
的形式显示在日志中

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

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

}

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

你为什么不做另一个初始值设定项呢

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

是否链接/连接了imageView?请尝试在imageView.image.的正上方登录。。。。。是打印一些数字还是null/nill?@AKV数据以“”的形式显示在日志中。您为什么要手动将控制器添加到导航控制器,而不是使用performsgue和prepareforsgue?Mikhail,因为我使用从中调用的自定义单元格。很遗憾,这没有帮助((我不明白为什么从传输的URL下载图片(“显示数据”),但不会发布!请尝试在配置视图中检查_imageView!=nil,如果是-检查您的IBOutlets,或您创建ITI的位置如果Mikhail是正确的(很可能是正确的),则在最终加载视图之前调用您的configureView。如果是这种情况,则从NIB加载视图(或分别在执行loadView时)将清除\u imageView的内容。此外,将首次分配\u imageView。obj-c的奇怪之处在于,
\u imageView.image=image;
将在\u imageView为nil时执行nicley。但它当然不会显示任何内容。@Mikhail我选中了-\u imageView.image=nil@HermannKlecker对不起,我做了一点也不懂你的想法(不太懂英语)。我该怎么办?