ios - 从 UIImageView 类访问 UINavigationViewController

标签 ios

我有一个带有自定义类的 UIImageView。当按下图像时,类应该推送一个新的 View Controller ,但是我似乎无法获得对当前 UINavigationController 的引用。我通常会做 self.navigationController,但在这里我不能。 我尝试了以下代码,但它不起作用:

[self.window.rootViewController.navigationController pushViewController:browser animated:YES];

最佳答案

给你的自定义 ImageView 一个委托(delegate):

@protocol CustomImageViewDelegate;
@interface CustomImageView : UIImageView
    @property (weak, nonatomic) id<CustomImageViewDelegate> delegate;
@end
@protocol CustomImageViewDelegate
    - (void)imageViewPressed:(CustomImageView *)imageView
@end

使您的 View Controller 成为委托(delegate)。按下图像时,您执行 [self.delegate imageViewPressed:self

您的 View Controller 将实现:

- (void)imageViewPressed:(CustomImageView *)imageView
{
    [self.navigationController pushViewController:browser animated:YES];
}

或者给你的 View 一个 Navigation Controller 属性:

@interface CustomImageView : UIImageView
    @property (weak, nonatomic) UINavigationController navigationController;
@end

然后您可以直接在自定义 ImageView 上使用[self.navigationController pushViewController:browser animated:YES]

您必须设置 navigationController 属性,iOS 不会为您做这件事。

在你的 Controller 上添加一行

self.yourCustomImageViewProperty.navigationController = self.navigationController;

这是一个非常基本的回答,但我希望它能引导您朝着正确的方向前进。

关于ios - 从 UIImageView 类访问 UINavigationViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23726338/

相关文章:

iOS 控制音量

iphone - 使用 ID 进行非消耗性应用内购买

ios - 如何在 iPhone SDK 3.0 中使用 HTTP Live Streaming 协议(protocol)

ios - 如何在现有图像上添加水印

iphone - 我怎样才能准确找到我的代码设计身份?

iphone - 如何使 NSUserDefault 设置与应用程序内设置保持同步?

ios - xcode/ Storyboard : can't drag bar button to toolbar at top

iphone - Viewcontroller 失败所以调用另一个 Viewcontroller - Objective-c

iphone - 收到推送通知后更改选定的选项卡

iOS TableView 滑动 UIContextualAction 无法获取正确的图像