Ios viewDidLoad在初始化后被调用

Ios viewDidLoad在初始化后被调用,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我的主ViewController嵌入到navigation controller中,在我的ViewController中,当我点击custom collectionViewCell时,它进入下一个View controller使用push segue,在我的第二个View controller中,我有UIImage属性 当我点击Collection ViewCell -(void)collectionView:(UICollectionView *)collectionView didSele

我的主
ViewController
嵌入到
navigation controller
中,在我的
ViewController
中,当我点击
custom collectionViewCell
时,它进入下一个
View controller
使用
push segue
,在我的第二个
View controller
中,我有
UIImage
属性

当我点击
Collection ViewCell

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Why did you called me?");
    DetailViewViewController *dvc = [[DetailViewViewController alloc] init];

    ALAsset *asset = self.assets[indexPath.row];
    ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];

    [dvc initWithImage:image];

}
initWithImage
方法

-(id) initWithImage:(UIImage *)imageName {
    self = [super init];
    NSLog(@"inithWithImage Called");

    self.image = imageName;

    return self;
}
问题:问题是
initWithImage
viewDidLoad
之前被调用。在initWithImage方法中,我将我的属性
UIImage
设置为
image
。但是当我签入
viewDidLoad
我的属性
UIImage
等于
null


如何解决此问题?

图像需要是segue使用的DetailViewController实例的属性,请查看“准备segue”

问题是,尽管您使用的是segues,但您还是手动alloc/init一个控制器并设置其属性(当然,这与将要展示的实例完全不同)


查看并以错误的方式初始化
DetailViewController
。尝试:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{
    ALAsset *asset = self.assets[indexPath.row];
    ALAssetRepresentation *defaultRep = [asset defaultRepresentation];

    UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] 
                                         scale:[defaultRep scale] 
                                   orientation:0];

    DetailViewViewController *dvc = [[DetailViewViewController alloc] initWithImage:image];

    ...
}

只需在DetailViewController中声明一个变量,并在分配该变量的实例时使用图像路径字符串/图像名称对其进行初始化。在viewdidload中,使用路径字符串/图像名称将DetailViewController设置为imageview组件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Why did you called me?");
    DetailViewViewController *dvc = [[DetailViewViewController alloc] init];

    //get the path of image
    ....
    ....
    dvc.variablename = imagepath / imagename
}

希望这有帮助。

image
设置为
detailviewcontroller
的一个属性,在这里您可以初始化
detailviewcontroller
两次
self.image=imageName
你可以看到image is property(非原子且强)我阅读了答案并创建了
DetailViewController
的属性,在
prepareforsgue
中初始化了该属性,并使用该属性调用了init,但在init之后调用了viewDidLoad,但是这次我的属性
UIImage
不等于
null
。但静止图像并没有显示出奇怪的效果,至少你使用的是正确的方法-。您是否正确分配了映像?是的,我认为问题在于在init之后调用viewDidLoad您应该使用init或initWithImage,而不是同时使用两者。但是,当使用segues时,我更喜欢使用我的方法,然后在viewDidLoad中对传递的属性执行一些操作,例如…您能检查DetailViewController的viewDidLoad中的imagepath字符串吗?您能解释一下这有什么错吗?我试过了,但没有效果