objective-c - 帮助访问 Objective C iOS 中的属性

标签 objective-c ios properties

我有一个基于导航的项目,我希望能够访问该项目不同类中的属性。

在我的 appDelegate 中,我有以下属性:

UINavigationController *navig;

然后我设置rootViewController:

MainViewController *theMainViewController = [[MainViewController alloc] init];
self.navig = [[UINavigationController alloc] initWithRootViewController:theMainViewController];

theMainViewController 对象中,我有以下属性:

MyScrollView *scrollView;

因此,要从不同的类访问此 scrollView 属性,我尝试了以下操作:

MyAppDelegate *myAppDelegate = (MyAppDelegate*) [UIApplication sharedApplication].delegate;
UIViewController *mainViewController = [myAppDelegate.navig.viewControllers objectAtIndex:0];
UIScrollView *scroll = mainViewController.scrollView;

但是,它不起作用。它似乎没有找到 scrollView 属性。难道我做错了什么?非常感谢!

最佳答案

如何拒绝属性(property)?

您还应该设置适当的 mainViewController 类型:它应该是 MainViewController:

MainViewController *mainViewController = [myAppDelegate.navig.viewControllers objectAtIndex:0];

关于objective-c - 帮助访问 Objective C iOS 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442768/

相关文章:

ios - 在 iOS 上实现体积图

ios - 当用户将应用程序从 watch OS1 升级到 watch OS2 时,我的 Watch OS2 应用程序未显示在 iPhone 的 'My Watch' 应用程序中

ios - 一些 unicode 箭头 (❯) 不显示

iphone - UITabBar(Controller) - 获取点击的索引?

iphone - 获取@interface 的所有变量名或属性

objective-c - GCC 有 GUI 吗?

ios - KVO 使应用程序崩溃

jdbc - Camel : Aggregator doesn't persist Exchange properties

c# - 如何使用一个属性来初始化另一个属性?

jsf - c :set for bean properties