2016-05-25 21 views
0

我試圖從第一個視圖控制器傳遞UIImage到第二個視圖控制器,但這不會出現在第二個視圖。爲什麼?OBJECTIVE-C:PushViewController與UIImage不起作用

我在FirstController.m代碼:

SecondController *second = (SecondController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"]; 

second.imgCard.image = [UIImage imageNamed:@"image.png"]; 

[self.navigationController pushViewController:second animated:YES]; 

我在SecondController.h代碼:

@property (nonatomic, strong) IBOutlet UIImageView *imgCard; 

我在SecondController.m代碼:

@implementation SecondController 

@synthesize imgCard; 

回答

3

如果你想在你的ImageView設置的圖像是在你的Assets.xcassets或在項目文件夾,比你不需要通過圖像prapare爲sague只是使用如下;

imgCard.image = [UIImage imageNamed:@"image.png"]; 
在FirstController.m

SecondController *second= (SecondController*)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"]; 

second.imageVar = [UIImage imageNamed:@"image.png"]; 

[self.navigationController pushViewController:second animated:YES]; 
在SecondController.h

@property (nonatomic, strong) IBOutlet UIImageView *imgCard; 
@property UIImage *imageVar; 

在SecondController.m:

@implementation SecondController 

@synthesize imageVar,imgCard; 

-(void)viewDidLoad{ 
[super viewDidLoad]; 
imagCard.image = imageVar; 
} 
+0

非常感謝!這是解決方案:) @Subhash Sharma – David

+0

:)開心的編碼 –

1

你的第一線代碼是錯誤的。要初始化你的第二個視圖控制器正確的方法是:

SecondController *second = (SecondController*)[self.storyboard instantiateViewControllerWithIdentifier:@"SBSecondController"]; 
second.imgCard.image = [UIImage imageNamed:@"image.png"]; 
... 
+0

謝謝,但我的問題沒有解決。 @stefos – David

+0

不要直接在Second的imageView中設置圖像。只傳遞圖片。並在SecondController中將viewDidLoad設置爲imageView。 – stefos

2

imgCardnil直到秒ond視圖控制器的視圖已被加載。這不會發生,直到您推視圖控制器。視圖控制器的視圖不會創建,並且直到需要時纔會連接插座。

您有兩種選擇。

  • pushViewController調用後設置圖像視圖的圖像(不知道這會造成視覺干擾)
  • image屬性添加到SecondController和設置。在SecondControllerviewDidLoad中,設置imgCard.image = self.image;