UIPopover控制器顶部的UIButton消失了

时间:2012-03-04 15:24:13

标签: ios xcode ipad uipopovercontroller uibarbuttonitem

我正在为iPAD制作照片幻灯片放映应用。我让用户通过弹出打开UIPopoverController来选择他们的照片。在那个弹出窗口中,我添加了一个名为“完成”的自定义按钮。现在这里变得古怪了。当弹出窗口第一次启动时,您会看到“完成”按钮。当我点击相册时,完成按钮消失,如果我回到主要照片选择页面,它也会从那里消失。见截图。

enter image description here

enter image description here

enter image description here

这是我正在使用的代码。世界上到底发生了什么?我想我的代码是正确的。

-(IBAction)selectExitingPicture
{
    //Specially for iPAD
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];

    popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [popoverController presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 300.0) 
                             inView:self.view
           permittedArrowDirections:UIPopoverArrowDirectionAny 
                           animated:YES];

}


- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    UINavigationItem *ipcNavBarTopItem;

    NSLog(@"Inside navigationController ...");

    // add done button to right side of nav bar
    doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain  target:self action:@selector(saveImages:)];

    bar = navigationController.navigationBar;
    [bar setHidden:NO];
    ipcNavBarTopItem = bar.topItem;
    ipcNavBarTopItem.rightBarButtonItem = doneButton;

}

1 个答案:

答案 0 :(得分:2)

您可以从传递给viewController方法的UINavigationControllerDelegate变量中获取您要查找的导航项,而不是自己计算出顶部导航项。此外,您可以缓存doneButton以避免每次图像选择器的顶视图控制器发生更改时重新创建它:

- (void)navigationController:(UINavigationController *)navigationController
        willShowViewController:(UIViewController *)viewController
        animated:(BOOL)animated
{
    if (!doneButton) {
        doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                      style:UIBarButtonItemStylePlain
                      target:self action:@selector(saveImages:)];
    }

    viewController.navigationItem.rightBarButtonItem = doneButton;
}