ios ipad UIPopoverController显示为空,没有错误

ios ipad UIPopoverController显示为空,没有错误,ios,ipad,uiviewcontroller,uipopovercontroller,Ios,Ipad,Uiviewcontroller,Uipopovercontroller,我正在尝试实现一个UIPopoverController来共享,但是当UIPopoverController为时,视图显示为空,并且我没有发现任何错误。这是我的密码: if ([self.activityPopoverController isPopoverVisible]) { [self.activityPopoverController dismissPopoverAnimated:YES]; } else { NSString *textToShare=@"I'm sh

我正在尝试实现一个UIPopoverController来共享,但是当UIPopoverController为时,视图显示为空,并且我没有发现任何错误。这是我的密码:

if ([self.activityPopoverController isPopoverVisible]) {
    [self.activityPopoverController dismissPopoverAnimated:YES];
} else {

    NSString *textToShare=@"I'm sharing this image";
    NSArray *activityItems = [[NSArray alloc]initWithObjects:self.imgToSend,textToShare,nil];
    UIActivityViewController *activityVC=[[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
     activityVC.excludedActivityTypes=@[UIActivityTypeAssignToContact,UIActivityTypeCopyToPasteboard ];


    activityVC.completionHandler = ^(NSString *activityType, BOOL completed){
        [self.activityPopoverController dismissPopoverAnimated:YES];
    };

    if (self.activityPopoverController) {
        [self.activityPopoverController setContentViewController:activityVC];
    } else {
        self.activityPopoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC];
    }
    [self.activityPopoverController presentPopoverFromRect:[(UIControl *)sender frame]
                                                    inView:self.view
                                  permittedArrowDirections:UIPopoverArrowDirectionAny
                                                  animated:YES];

} 
你们中的任何人都知道为什么我的代码有问题

我真的很感谢你的帮助

更新:

我添加这行代码:

 [self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover animated:YES];
但看起来都被砍掉了:

此处:

   NSArray *activityItems = [[NSArray alloc] 
                              initWithObjects:self.imgToSend,textToShare,nil];
   UIActivityViewController *activityVC=[[UIActivityViewController alloc]
                                          initWithActivityItems:activityItems 
                                          applicationActivities:nil];
如果
self.imgToSend
nil
activityItems
将为
nil
,您将获得一个空的弹出窗口。因此,您需要检查
self.imgToSend
是否已初始化。请尝试
NSLog(“imgToSend:%@”,self.imgToSend)
检查此案例

数组为nil是因为
nil
是数组的终止符,因此如果第一个对象是
nil
,系统会认为数组在该点完成,不会超出它(例如,到
textToShare

正如文件所说:

活动项目

此数组不能为nil,并且必须至少包含一个对象

您可以通过交换两个数组项来检查我是否正确:

  NSArray *activityItems = [[NSArray alloc] 
                            initWithObjects:textToShare,self.imgToSend,nil];
更新
在您的更新问题中,您介绍了这一行:

[self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover     
                                             animated:YES];
您声称这通过使用项目填充来修复活动PopoverController,但控制器的框架设置不正确

这一行确实改变了popOver的框架,但由于所有错误的原因-您任意将框架设置为主机viewController的contentSizeForViewInPopover-而不是popOver中显示的viewController。您不妨在其中输入一些任意数字,例如:

[self.activityPopoverController setPopoverContentSize:(CGSize){300,400} 
                                             animated:YES];
这也将重塑您的popover-但在任何情况下,此代码都不对activityItems的存在或不存在负责

如果ActivityItems现在出现,那么您一定也更改了其他内容。我建议您现在尝试在注释掉这一行的情况下运行代码:

// [self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover     
//                                              animated:YES];
并报告发生的情况

我确信您的问题在于
self.imgToSend
。如果我注意用本地设置的图像对象替换
self.imgToSend
,我可以完全按照您的问题运行代码,并正确显示活动项

这成功:

     UIImage* image= [UIImage imageNamed:@"test.png"];
     NSArray *activityItems = [[NSArray alloc]initWithObjects:image,@"text",nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",image,nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",nil];
此操作失败(空的popOverController):

这成功:

     UIImage* image= [UIImage imageNamed:@"test.png"];
     NSArray *activityItems = [[NSArray alloc]initWithObjects:image,@"text",nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",image,nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",nil];
这成功:

     UIImage* image= [UIImage imageNamed:@"test.png"];
     NSArray *activityItems = [[NSArray alloc]initWithObjects:image,@"text",nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",image,nil];
     UIImage* image= nil;
     NSArray *activityItems = [[NSArray alloc]initWithObjects:@"text",nil];

我建议你拿出
self.imgToSend
看看你的activityItems在没有它的情况下是否正确填充,那么至少你会知道问题所在。

你是否在UIActivityViewController上设置ContentSizeInPover属性?我不是:(我该怎么做?@HelenaM,我不认为这是原因,请检查我的答案。contentSize技巧具有误导性,您不应该设置它。系统会为activityViewController的弹出窗口计算contentSize。您任意将其设置为viewController视图的contentSize,这是不正确的。我确信您的问题是图像将附加到消息中,但不为零。activityitems数组也不为零nill@Helena,您能否在问题中添加您的
活动项的
NSLog
。如果您只是从数组中删除self.imgToSend,会发生什么情况?它看起来如何?我认为您没有抓住要点。问题不在于array。问题是UIPopoverController在视图中的显示方式。@HelenaM,您报告了一个空的popOver视图,这是由一个空数组引起的。并且只要img变量的内容正确,您的代码在我的系统上就可以正常工作,所以我仍然怀疑该变量有问题。如果您只是删除它,pop将如何处理over look?(没有那种
contentsizeforviewinpover
干扰)。您更新的问题(带有图像)不会显示空视图,这里可能有不止一个问题。问题不在于您报告的代码。