如何从其他类访问根视图控制器中的方法?

编程入门 行业动态 更新时间:2024-10-26 22:16:30
本文介绍了如何从其他类访问根视图控制器中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

很抱歉这很简单.

我有我的根视图控制器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.

  • 更多推荐

    如何从其他类访问根视图控制器中的方法?

    本文发布于:2023-11-17 04:24:33,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1608820.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:视图   器中   方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!