0

我创建了一个自定义导航控制器,我称之为CatalogueNavigationController. 这CatalogueNavigationController有一个名为currentLevelwhich 的属性是 type int

在此导航控制器中显示的我的一个视图控制器上,我想获得CatalogueNavigationController'currentLevel值。但是,当我尝试使用 引用它时self.navigationController.currentLevel,我遇到了一个错误,表明 currentLevel 不是 的属性UINavigationController- 我知道,它是CatalogueNavigationController.

目录导航控制器

// .h
@interface CatalogueNavigationController : UINavigationController
@property int currentLevel;

// .m
@implementation CatalogueNavigationController
@synthesize currentLevel;

初始化视图控制器

CatalogueBrowser *catalogue = [[CatalogueBrowser alloc] initWithStyle:UITableViewStyleGrouped andQuery:nil];
CatalogueNavigationController *navigation = [[CatalogueNavigationController alloc] initWithRootViewController:catalogue];
navigation.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navigation animated:YES];

目录浏览器视图控制器

NSLog(@"%i",self.navigationController.currentLevel); //ERROR: Property 'currentLevel' not found on object of type 'UINavigationController *'

currentLevel 变量在 viewController 弹出时递减,而在 viewController 被推送时递增,以允许我跟踪用户导航的“深度”。所以我需要返回它的当前状态。

我可以使用通知来结合我的视图控制器和 CatalogueNavigationController 上的变量,但肯定有更优雅的解决方案吗?

如何引用 self.navigationController 来返回 的值currentLevel,但让它引用CatalogueNavigationController而不是 UINavigationController?

4

2 回答 2

1

我认为最好的解决方案是测试 navigationController 是否属于您想要的类型,这里解释:在 Objective-C 中,如何测试对象类型?. 如果它是真的,你将它转换为你的对象,然后检查你想要的值。

铸造对象中的信息:ios 自定义 NSObject 铸造

希望能帮助到你!

于 2012-06-22T15:00:08.067 回答
0

我偶然发现了同样的情况。我认为解决方案是这样的:

CatalogueNavigationController *navController = (CatalogueNavigationController *)self.navigationcontroller;

NSLog(@"%i", navController.currentLevel);
于 2014-03-11T15:02:39.970 回答