繁体   English   中英

在 iOS 13 中将静态委托设置为 UIApplication (objective-c)

[英]Set static delegate to UIApplication in iOS 13 (objective-c)

我不太了解objective-c,我正在尝试定义一个应用程序委托以在其他类中使用它。

经过搜索并根据一些示例,我发现我必须创建一个静态属性来保存委托:

.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
+(AppDelegate *)sharedAppDelegate;


.m:
static AppDelegate *appDelegate = nil;
+(AppDelegate *)sharedAppDelegate{
    static dispatch_once_t pred;
    static AppDelegate *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[super alloc] init];
    });
    return shared;
}

然后将委托设置为该静态属性 (sharedAppDelegate):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setDelegate:[AppDelegate sharedAppDelegate]];

我在 ios 11 和 12 上使用了这段代码并且工作了,但是在 ios 13(xcode 11.3.1)中,当我设置委托应用程序屏幕变黑并且 xcode 显示这些错误时:

Thread 1: signal SIGABRT


libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有什么问题?

最后,我想打开 ios 弹出页面以将文本共享给其他应用程序,例如电报或 whatsapp 或...我需要该委托才能在我称为共享函数的其他类上使用它:

void IOS::shareText(const QString &text) {

NSMutableArray *sharingItems = [NSMutableArray new];

    if (!text.isEmpty()) {
        [sharingItems addObject:text.toNSString()];
    }

    // get the main window rootViewController
    UIViewController *self = [[UIApplication sharedApplication].keyWindow rootViewController];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
    if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
          activityController.popoverPresentationController.sourceView = self.view;
    }
    [self presentViewController:activityController animated:YES completion:nil];

}

我在qt中使用混合objective-c和c++

我不太了解objective-c,我正在尝试定义一个应用程序委托以在其他类中使用它。

经过搜索并根据一些示例,我发现我必须创建一个静态属性来保存委托:

.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
+(AppDelegate *)sharedAppDelegate;


.m:
static AppDelegate *appDelegate = nil;
+(AppDelegate *)sharedAppDelegate{
    static dispatch_once_t pred;
    static AppDelegate *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[super alloc] init];
    });
    return shared;
}

然后将委托设置为该静态属性 (sharedAppDelegate):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setDelegate:[AppDelegate sharedAppDelegate]];

我在 ios 11 和 12 上使用了这段代码并且工作了,但是在 ios 13(xcode 11.3.1)中,当我设置委托应用程序屏幕变黑并且 xcode 显示这些错误时:

Thread 1: signal SIGABRT


libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

有什么问题?

最后,我想打开 ios 弹出页面以将文本共享给其他应用程序,例如电报或 whatsapp 或...我需要该委托才能在我称为共享函数的其他类上使用它:

void IOS::shareText(const QString &text) {

NSMutableArray *sharingItems = [NSMutableArray new];

    if (!text.isEmpty()) {
        [sharingItems addObject:text.toNSString()];
    }

    // get the main window rootViewController
    UIViewController *self = [[UIApplication sharedApplication].keyWindow rootViewController];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
    if ( [activityController respondsToSelector:@selector(popoverPresentationController)] ) { // iOS8
          activityController.popoverPresentationController.sourceView = self.view;
    }
    [self presentViewController:activityController animated:YES completion:nil];

}

我在qt中使用混合objective-c和c++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM