加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – AppDelegate到UIViewController

发布时间:2020-12-14 17:38:13 所属栏目:百科 来源:网络整理
导读:我有一个带有标题和实现文件的示例代码以及两个appDelegate文件.我想将项目的功能添加到我的应用程序中.前两个文件是一个ViewController文件,所以我只需要将其拖入,但其他两个是AppDelegate,我显然不能有两个应用程序代理.但是在这个示例应用程序的情况下,ap
我有一个带有标题和实现文件的示例代码以及两个appDelegate文件.我想将项目的功能添加到我的应用程序中.前两个文件是一个ViewController文件,所以我只需要将其拖入,但其他两个是AppDelegate,我显然不能有两个应用程序代理.但是在这个示例应用程序的情况下,app委托被用作正确的viewcontroller,因为在UIViewController文件的.m文件中,有以下代码:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

由于AppDelegate没有实现applicationWillBecomeActive:等方法,我如何将文件转换为UIViewController文件?我需要在上面的代码中更改以调用控制器而不是代理(我的AppDelegate将保持不变).

代码是GitHub

这就是我所做的(代码需要包含Facebook API).转到下载选项卡,下载Archive.zip和AppDelegate.zip:https://github.com/Alexmitico45/FacebookRequests/downloads

基本上,控制器ContactFBSViewController链接到故事板中的viewcontroller.

解决方法

如果我理解正确,您希望示例中AppDelegate的功能在您的应用程序中.但您不想更换应用程序现有的AppDelegate?

AppDelegate类与其他类没有任何不同.它与众不同之处在于它实现了UIApplicationDelegate,并在main.m中明确引用,作为应用程序的启动类.

如果你想将它转换成另一个类,我会:

>重命名类(.h,.m,接口和实现名称),然后将其放入项目中.
>在NewName.h中,删除顶部的UIApplicationDelegate实现.
>在NewName.m中,您希望查看UIApplication方法部分中的任何功能,因为该功能专门分配给AppDelegate类执行,并查看是否需要将其合并到现有的AppDelegate类中.然后从NewName.m中删除那些UIApplicationDelegate方法,因为它们只能在您实现UIApplicationDelegate时存在.

由于ViewController利用了其预期的AppDelegate是UIApplication层次结构中的长期类的事实,因此您需要复制该行为.这可能是通过在ViewController.h中创建一个引用NewName的新弱属性来实现的:

ViewController.h

@class NewName;
@property (nonatomic,weak) NewName *newNameDelegate;

ViewController.m

#import "NewName.h"

...

@implementation ViewController
@synthesize newNameDelegate;

...

- (IBAction)sendRequestButtonAction:(id)sender {
    if (FBSession.activeSession.isOpen) {
        [newNameDelegate sendRequest];
    }
}

NewName.m

...

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.viewController.newNameDelegate = self;

...

希望这能让您了解从哪里开始.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读