iOS 侦听 UIPopoverPresentationController 关闭事件

iOS listen for UIPopoverPresentationController close event

提问人:Sean McManus 提问时间:6/16/2023 更新时间:6/16/2023 访问量:37

问:

我在弹出窗口中有一个共享对话框,我想在关闭时触发回调。

我尝试将委托分配给 UIPopoverPresentationController,但当我关闭应用程序中的共享对话框时,没有触发任何回调。

我将回调委托定义为 PicklePopupDelegate,我只有这 3 个函数用于测试,它们都没有被触发。

@implementation PicklePopupDelegate

- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController {
    // Share activity was closed, handle the event here
    NSLog(@"Share activity closed");
    
    UnitySendMessage("ShareManager", "OnSharePromptClosedIOS", "");
}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
    // Share activity was closed, handle the event here
    NSLog(@"Share activity closed");
    
    UnitySendMessage("ShareManager", "OnSharePromptClosedIOS", "");
}

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController {
    NSLog(@"Showing share activity");
}

@end

然后,此处定义了主共享函数,除了分配给 UIPopoverPresentationController 的委托在共享对话框关闭时未触发任何回调外,一切似乎都正常工作。

@implementation PicklePlugin

-(PicklePlugin*) initShareMessage:(char*)message initShareSubject:(char*)subject initDoShareIcon:(bool*)doShareIcon{
    // Set self to the reference of the original caller
    self = [super init];
    
    // Create a new array named items
    NSMutableArray *items = [NSMutableArray new];
    
    if(strlen(message) > 0){
        // Add the message to the item array
        [items addObject:[NSString stringWithUTF8String:message]];
    }
    
    if(doShareIcon){
        UIImage *appIcon = [UIImage imageNamed: [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIconFiles"] objectAtIndex:0]];
        
        if(appIcon != nil)
            [items addObject:appIcon];
    }
    
    // Initialize a new activity https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622019-initwithactivityitems
    UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    
    if(strlen(subject) > 0){
        // Set the value of a key name "subject" to the subject parameter we sent on the activity we created
        [activity setValue:[NSString stringWithUTF8String:subject] forKey:@"subject"];
    }
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    // While the rootViewController has a parent set it to be the parent until it eventually no longer has a parent
    // https://pinkstone.co.uk/how-to-avoid-whose-view-is-not-in-the-window-hierarchy-error-when-presenting-a-uiviewcontroller/
    while(rootViewController.presentedViewController)
        rootViewController = rootViewController.presentedViewController;    
    
    // Present the view controller using the popover style
    activity.modalPresentationStyle = UIModalPresentationPopover;
    [rootViewController presentViewController:activity animated:YES completion:nil];
    
    // Create an instance of PicklePopupDelegate
    PicklePopupDelegate *popoverDelegate = [[PicklePopupDelegate alloc] init];
    
    // Get the popover presentation controller and configure it
    UIPopoverPresentationController *presentationController = [activity popoverPresentationController];
    presentationController.permittedArrowDirections = 0;
    presentationController.sourceView = rootViewController.view;
    presentationController.sourceRect = CGRectMake(CGRectGetMidX(rootViewController.view.bounds), CGRectGetMidY(rootViewController.view.bounds), 0, 0);
    
    presentationController.delegate = popoverDelegate;
    
    return self;
}

// stripped out other functions
@end

在头文件中,我将类定义为:

@interface PicklePopupDelegate : UIViewController <UIPopoverPresentationControllerDelegate>
@end

@interface PicklePlugin : UIViewController <UIPopoverPresentationControllerDelegate>
// more code here
@end
iOS Objective-C UIViewController 委托 UIPopOverPresentationController

评论

0赞 HangarRash 6/16/2023
属性是。您创建的实例没有强引用,因此它会在完成后立即解除分配。这就是不调用任何委托方法的原因。UIPopoverPresentationController delegateweakPicklePopupDelegateinitShareMessage:

答: 暂无答案