很抱歉这很简单.
我有我的根视图控制器NB_ViewController.我还有一个名为ShowScreen的类. ShowScreen如何访问NB_ViewController中的方法?
I have my root view controller NB_ViewController. I have another class named ShowScreen. How can ShowScreen access a method in NB_ViewController?
非常感谢您.
推荐答案
在您的应用程序委托中创建全局"属性,该属性保留对根视图控制器的引用,例如@property(nonatomic, retain) UIViewController *rootViewController
在应用程序委托的-applicationDidFinishLaunching:方法中实例化根视图控制器时,将self.rootViewController属性设置为等于新实例化的视图控制器
When you instantiate the root view controller in the app delegate's -applicationDidFinishLaunching: method, set the self.rootViewController property equal to the newly instantiated view controller
在常量文件中(或要使用它的类标题中)添加#define宏,使您可以从任何地方访问应用程序委托,例如#define UIAppDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)
Add a #define macro in your constants file (or in the class header where you want to use it) that lets you access the application delegate from anywhere, e.g. #define UIAppDelegate ((MyAppDelegate *)[UIApplication sharedApplication].delegate)
在ShowScreen中,通过应用程序委托调用根视图控制器的属性或方法,例如[[UIAppDelegate rootViewController] someRootVCProperty]或[[UIAppDelegate rootViewController] someRootVCMethod:foo withArgument:bar]
In ShowScreen, call a property or method of the root view controller through the app delegate, e.g. [[UIAppDelegate rootViewController] someRootVCProperty] or [[UIAppDelegate rootViewController] someRootVCMethod:foo withArgument:bar]
如果使用Interface Builder,并且相应的根视图控制器IBOutlet称为rootViewController,则
步骤2应该是可选的.
Step 2 should be optional if you use Interface Builder and the corresponding root view controller IBOutlet is called rootViewController.
更多推荐
如何从其他类访问根视图控制器中的方法?
发布评论