我在 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/