Properties Objective-C:将self.navigationController与自定义UINavigationController一起使用

Properties Objective-C:将self.navigationController与自定义UINavigationController一起使用,properties,uinavigationcontroller,custom-component,Properties,Uinavigationcontroller,Custom Component,我已经创建了一个自定义导航控制器,我称之为catalogenavigationcontroller。此catalogenavigationcontroller有一个名为currentLevel的属性,其类型为int 在此导航控制器中显示的其中一个视图控制器上,我希望获得catalogenavigationcontroller的currentLevel值。但是,当我尝试使用self.navigationController.currentLevel引用它时,我遇到了一个错误,表明currentLe

我已经创建了一个自定义导航控制器,我称之为
catalogenavigationcontroller
。此
catalogenavigationcontroller
有一个名为
currentLevel
的属性,其类型为
int

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

目录激活控制器

// .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 *'
初始化视图控制器

// .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 *'
目录浏览器视图控制器

// .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 *'
当弹出viewController时,currentLevel变量递减;当按下viewController时,currentLevel变量递增,以便跟踪用户在导航中的“深度”。所以我需要返回它的当前状态

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


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

我认为最好的解决方案是测试navigationController是否属于您想要的类型,此处解释:。如果是真的,则将其投射到对象,然后检查所需的值

强制转换对象中的信息:


希望有帮助

我偶然发现了同样的情况。我认为解决办法是:

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

NSLog(@"%i", navController.currentLevel);

为此干杯,但以这种方式强制转换将始终检索初始化版本,而不是当前实例。在我的CatalogenavigationController中,我递增/递减NSLog正确输出的currentLevel,使用此方法,它总是返回0。