带有照片和图库按钮的 iOS 全局菜单

标签 ios objective-c uiimagepickercontroller appdelegate

我在 AppDelegate 中添加了底部菜单:

- (void)addBottomMenu
{
    BottomMenu *bottomMenu = [[BottomMenu alloc] initWithFrame:CGRectMake(0, [ScreenHelper getHeightOfScreen] - 70, 320, 70)];
    bottomMenu.delegate = self;
    [[[[UIApplication sharedApplication]  delegate] window] addSubview:bottomMenu];
}

菜单中有 2 个按钮,用于从图库拍照和相机拍照。我希望这个按钮在全局上工作相同(在每个 Controller 中)。它应该可以显示图库(或相机),并且在用户选择(或拍摄)照片后,它会导航到同一 Controller 并显示图像。在 Controller 中我将添加这些代码行:

- (void)photoButtonTapped
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark Image Picker delegates
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imageOfReceipt = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:nil];
    [self performSegueWithIdentifier:@"detailReceiptSegue" sender:self];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

...其中 photoButtonTapped 来自委托(delegate)。它正在工作(当我在 Controller 中尝试底部菜单时,而不是现在当我添加到 AppDelegate 时)。在 AppDelegate 中,呈现 View Controller 存在问题(我知道为什么,但我不知道如何绕过这个问题)。我需要让它在 AppDelegate 中工作。我不想将其复制到每个 Controller 并更改 AppDelegate 中 BottomMenu 中的委托(delegate)。可以在全局范围内进行吗?

最佳答案

是的,是的。为什么不打电话:

[self.navigationController.topViewController presentViewController:picker 
                                                          animated:YES 
                                                        completion:NULL];

如果 AppDelegate 中没有 navigationController 属性,尝试通过其他方式获取当前呈现的 View Controller。例如尝试 self.window.rootViewController。你可能应该放弃使用 segues。

关于带有照片和图库按钮的 iOS 全局菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952644/

相关文章:

ios - 代号一中的 DNS 问题

ios - 来自 Parse 的图像未在 iOS 9 上加载

ios - 将 NSString 数组显示为带有 CGContext 的圆(有效),但没有角度?

iPhone 配置实用程序 - 禁用相机应用程序还会禁用 ImagePicker

ios - 在 UIImagePickerController 之上呈现一个 View Controller ,而不是关闭 UIImagePickerController

ios - Google Analytics 发送命中时内存泄漏

ios - Sharekit - Facebook 配置

ios - Titanium iOS 模块构建成功但在测试工具中中断?

iphone - 检查 NSDictionary 是否为空

ios - 对尚未渲染的 View 进行快照会产生空快照。警告信息