ios - 如何将引用从调用 View Controller 传递到带有 segue 的 View Controller ?

标签 ios

这是我得到的:

我有一个带有图像数组的 Root View Controller 。我有一个 detailViewController,它只有一个用于显示全屏图像的全屏 UIImageView。我还定义了一个呈现 detailViewController 的 segue。当用户在 rootViewController 中选择图像时,将以编程方式调用此 segue,如下所示:

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    [self performSegueWithIdentifier:@"fullScreenSegue" sender:self];
}

问题是 detailViewController 有一个图像属性,它设置为它显示的 UIImageView 的图像,我需要一种方法将图像传递给它以显示。我该怎么做呢?根据苹果文档,我应该在调用 Controller 中实现 prepareForSegue 方法,但此时,我无法再访问上面的索引。这些图像在 Root View Controller 中的一个数组中,我需要该索引来知道要将哪个图像传递给 detailViewController。我还将 detailViewController 设置为具有一个协议(protocol),该协议(protocol)将调用 Controller 设置为委托(delegate),然后在 init() 方法中调用“setImage”方法,但这似乎根本不起作用,而且无论如何看起来都是一个疯狂的实现。 .

我考虑过在 rootViewController 中声明一个属性,例如 @property int selectedIndex,然后在上述方法中设置该属性,然后在 prepareForSegue 方法中执行如下操作:

detailViewController.imageForDisplay = [images objectAtIndex:selectedIndex];

这可能会奏效,但这种方法让我感到不对...

在此先感谢您的帮助!

最佳答案

两种处理方式。一个使用 prepareForSegue 并在调用 segue 之前将索引保存在某处...

self.selectedIndex = index;
[self performSegueWithIdentifier:@"fullScreenSegue" sender:self];

然后在 prepareForSegue 中使用:

if([[segue identifier] isEqualToString:@"fullScreenSegue"])
{
    DetailViewController *detailViewController = segue.destinationViewController;
    detailViewController.imageForDisplay = [self.images objectAtIndex:self.selectedIndex];
}

第二种方法是使用您的委托(delegate)和协议(protocol),但在您的 viewDidLoad 方法中调用 setImage,而不是在您的 init 中调用。

希望对您有所帮助。看起来你们已经很接近了。

关于ios - 如何将引用从调用 View Controller 传递到带有 segue 的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213858/

相关文章:

ios - 为 NSDate 添加 24 小时

iphone - Objective C 方法命名约定

ios - 如何使用 Swift 中的 editActionsForRowAtIndexPath 检测单元格是否被滑动到正常状态?

ios - 在 iOS 7 中使用 applicationDidEnterBackground : 应用程序进入后台时无法更新 View

ios - 使用 google 登录会阻止用户使用不同的帐户再次登录 - iOS

ios - 是否可以从 Objective-C (Xcode/iOS) 中的主类继承?

ios - Core Data 自定义元数据不为 iCloud 存储保存

objective-c - 显示视频和图像

ios - Armv7s 的 OpenCV 不包括合并或拆分方法?

ios - 如何将 PDF 作为输入传递给打印机