2012-11-30 75 views
0

我知道iPad上的圖片選取器需要成爲彈出式廣告素材,並且我一直在嘗試,但當我點擊調用彈出式廣告素材的按鈕時,它將無法正常工作並崩潰。ipad上的圖片選取器

-(IBAction)addPhoto:(id)sender{ 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    popover=[[UIPopoverController alloc] 
      initWithContentViewController:imagePicker]; 
    [popover presentPopoverFromRect:((UIButton *)sender).frame 
          inView:self.view 
      permittedArrowDirections:UIPopoverArrowDirectionAny 
          animated:YES]; 
} 


    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo { 

    // Delete any existing image. 
    NSManagedObject *oldImage = imageClass.image; 
    if (oldImage != nil) { 
     [imageClass.managedObjectContext deleteObject:oldImage]; 
    } 

    // Create an image object for the new image. 
    NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:imageClass.managedObjectContext]; 
    imageClass.image = myImage; 

    // Set the image for the image managed object. 
    [image setValue:selectedImage forKey:@"image"]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

哪部分不起作用? – Joe

+0

它崩潰的第二我點擊應該顯示彈出式窗口的按鈕 – user1858163

+0

什麼是在backtrace? – nielsbot

回答

1

根據您所得到的例外,問題是senderaddPhoto方法實際上是UIBarButtonItem,而不是一個UIButton。您需要更改彈出窗口的顯示方式,使用按鈕的框架顯示在欄按鈕上。

-(IBAction)addPhoto:(UIBarButtonItem *)sender { 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    popover = [[UIPopoverController alloc] 
     initWithContentViewController:imagePicker]; 

    [popover presentPopoverFromBarButtonItem:sender 
     permittedArrowDirections:UIPopoverArrowDirectionAny 
     animated:YES]; 
} 
+0

這工作,謝謝! – user1858163

0

嘗試創建屬性來存儲您的popover控制器或存儲內容控制器。似乎視圖控制器之一在方法結束後立即發佈。另外,iOS 7在彈出窗口中有UIImagePickerController的糟糕表現。 This is related question.