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字符串吗?您能解释一下这有什么错吗?我试过了,但没有效果