2013-04-10 48 views
1

我想實現一個UIPopoverController能夠共享,但是當UIPopoverController是視圖顯示爲空,我不會有任何錯誤。這是我的代碼:ios ipad UIPopoverController顯示爲空,沒有錯誤

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

    NSString *[email protected]"I'm sharing this image"; 
    NSArray *activityItems = [[NSArray alloc]initWithObjects:self.imgToSend,textToShare,nil]; 
    UIActivityViewController *activityVC=[[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; 
    [email protected][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]; 

} 

你們中的任何人都可能知道爲什麼這是我的代碼錯了?

我真的很感謝你的幫助。

UPDATE:

我加入這行代碼:

[self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover animated:YES]; 

,但它看起來都砍掉:

enter image description here

+0

你是否在UIActivityViewController上設置contentSizeInPopover屬性? – sixthcent 2013-04-10 22:50:02

+0

我不是:(我該怎麼做? – HelenaM 2013-04-11 01:05:33

+0

@HelenaM,我不認爲這是原因,請檢查我的答案。 – foundry 2013-04-11 01:22:50

回答

0

這裏:

NSArray *activityItems = [[NSArray alloc] 
           initWithObjects:self.imgToSend,textToShare,nil]; 
    UIActivityViewController *activityVC=[[UIActivityViewController alloc] 
              initWithActivityItems:activityItems 
              applicationActivities:nil]; 

如果self.imgToSendnilactivityItems將是nil,您將得到一個空的彈出窗口。所以你需要檢查self.imgToSend被初始化。嘗試NSLog("imgToSend: %@",self.imgToSend)來檢查這種情況。

數組是零,因爲nil是數組終結者,所以如果你的第一個對象是nil,系統會認爲你的陣列在這一點上完成,不超越它(到textToShare,例如)

而作爲文檔說:

activityItems

此數組必須不爲零,且必須包含至少一個對象。

您可以檢查我是否正確周圍交換你的兩個數組項:

NSArray *activityItems = [[NSArray alloc] 
          initWithObjects:textToShare,self.imgToSend,nil]; 

更新
在你更新的問題,在你引入了這一行:

[self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover  
              animated:YES]; 

,你聲稱這會通過填充項目來修復PopoverController活動,但控制器的框架設置不正確。

此行確實改變酥料餅的框架,但對所有錯誤的原因 - 你是框架任意設置爲您主機的viewController的contentSizeForViewInPopover - 這是正在顯示在酥料餅中的viewController。你還不如干脆把一些任意數字在那裏,例如:

[self.activityPopoverController setPopoverContentSize:(CGSize){300,400} 
              animated:YES]; 

這也將重塑你的酥料餅 - 但在兩種情況下是這樣的代碼負責activityItems的存在與否。

如果活動項目現在正在出現,那麼您必須改變其他項目。我建議你現在嘗試運行你的代碼與這條線註釋掉:

// [self.activityPopoverController setPopoverContentSize:self.contentSizeForViewInPopover  
//            animated:YES]; 

並報告會發生什麼。

我真的確定你的問題在於self.imgToSend。我可以運行你的代碼剛好,因爲它在你的問題中,如果我照顧用本地設置的圖像對象替換self.imgToSend,activiyItems顯示正確。

這將成功:

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

此失敗(空popOverController):

 UIImage* image= nil; 
    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是否正確填充沒有它,那麼至少你會知道問題出在哪裏。

+0

圖像是附加在消息中但不是nil – HelenaM 2013-04-11 01:23:23

+0

既不是數組activityitems不是沒有nill – HelenaM 2013-04-11 01:24:33

+0

@ Helena,你可以給你的問題添加一個'activityItems'的NSLog',如果你剛剛從數組中刪除self.imgToSend會怎麼樣呢? – foundry 2013-04-11 01:42:52