Ios 如何从自定义类访问变量?
下面是它在故事板中的外观: CustomTabBarController.h:Ios 如何从自定义类访问变量?,ios,Ios,下面是它在故事板中的外观: CustomTabBarController.h: #import <UIKit/UIKit.h> @interface CustomTabBarController : UITabBarController<UITabBarDelegate> @property NSUInteger tabIndex; @property NSUInteger pageIndex; @end #导入 @接口CustomTabBarController:
#import <UIKit/UIKit.h>
@interface CustomTabBarController : UITabBarController<UITabBarDelegate>
@property NSUInteger tabIndex;
@property NSUInteger pageIndex;
@end
#导入
@接口CustomTabBarController:UITabBarController
@属性指数;
@属性索引;
@结束
我在CustomTabBarController中存储了变量如何在RootViewController中访问它们?前提条件:您的属性需要在CustomTabBarController
界面(CustomTabBarController.h
)中公开:
您可以通过tabBarController中的任何控制器访问属性:
((CustomTabBarController *)self.tabBarController).object
您可以从选项卡栏控制器的viewControllers属性访问根视图控制器。您需要将其类型转换为适当的类
static NSString *const TabIdentifier = TAB_IDENTIFIER;
UITabBarController *tabBarController=[storyboard instantiateViewControllerWithIdentifier:TabIdentifier];
[(ViewController *)[tabBarController.viewControllers objectAtIndex:0] setData:mdata];
您可以将所需的变量从CustomTabBar传递到RootViewController,以便在创建RootViewController时使用。如果您可能在整个应用程序中使用这些值,那么将它们拆分为一个Singleton可能是值得的。我已经多次看到Singleton这个名称,我一定会看一看谢谢
static NSString *const TabIdentifier = TAB_IDENTIFIER;
UITabBarController *tabBarController=[storyboard instantiateViewControllerWithIdentifier:TabIdentifier];
[(ViewController *)[tabBarController.viewControllers objectAtIndex:0] setData:mdata];