在 ipad 中分享问题

sharing problems in ipad

当我尝试在社交网络中使用共享共享时出现问题,但出现错误错误:如下

2015-05-20 14:01:05.267 NovedadesQuintanaRoo[945:174927] Sharing2
2015-05-20 14:01:05.733 NovedadesQuintanaRoo[945:174927] -[UIButton view]: unrecognized selector sent to instance 0x7fb468d8bac0
2015-05-20 14:01:05.741 NovedadesQuintanaRoo[945:174927] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton view]: unrecognized selector sent to instance 0x7fb468d8bac0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000110d8ec65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001106cfbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000110d960ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000110cec13c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000110cebcd8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010fb17de5 -[UIPopoverPresentationController _sourceView] + 39
    6   UIKit                               0x000000010fb1685f -[UIPopoverPresentationController presentationTransitionWillBegin] + 287
    7   UIKit                               0x000000010f512d79 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1305
    8   UIKit                               0x000000010f5113c0 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 189
    9   UIKit                               0x000000010f42762c _applyBlockToCFArrayCopiedToStack + 314
    10  UIKit                               0x000000010f4274a6 _afterCACommitHandler + 533
    11  CoreFoundation                      0x0000000110cc1ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x0000000110cc1c00 __CFRunLoopDoObservers + 368
    13  CoreFoundation                      0x0000000110cb7a33 __CFRunLoopRun + 1123
    14  CoreFoundation                      0x0000000110cb7366 CFRunLoopRunSpecific + 470
    15  GraphicsServices                    0x0000000112e9ea3e GSEventRunModal + 161
    16  UIKit                               0x000000010f403900 UIApplicationMain + 1282
    17  NovedadesQuintanaRoo                0x000000010d8bf6df main + 111
    18  libdyld.dylib                       0x00000001119cc145 start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如果有人能帮助我,这是我的代码

- (IBAction)compartir:(id)sender{
    NSLog(@"Sharing2");

    NSString *text = self.story.title;
    NSURL *url = self.story.url;
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.story.thumb]];

    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[text, url, image] applicationActivities:nil];

    if ([controller respondsToSelector:@selector(popoverPresentationController)]) {
        controller.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;
    }

    [self presentViewController:controller animated:YES completion:nil];
}

如果我工作,请在 iPhone 上使用相同的代码

这是iPhone中的代码:

- (IBAction)share:(id)sender {
    NSLog(@"Sharing");

    NSString *text = self.story.title;
    NSURL *url = self.story.url;
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:self.story.thumb]];

    UIActivityViewController *controller =
    [[UIActivityViewController alloc]
     initWithActivityItems:@[text, url, image]
     applicationActivities:nil];

    [self presentViewController:controller animated:YES completion:nil];
}

您不能将 sender 转换为 UIBarButtonItem,因为 sender 实际上是 UIButton

换行:

controller.popoverPresentationController.barButtonItem = (UIBarButtonItem *)sender;

至:

controller.popoverPresentationController.sourceView = sender;