2014-07-25 47 views
0

我說我的底部菜單中的AppDelegate:有照片和畫廊的按鈕iOS的全局菜單

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

我在菜單2個按鈕取照片從畫廊和相機拍攝的照片。我想這個按鈕工作全球相同(在每個控制器)。它應該能夠顯示圖庫(或照相機),並且在用戶選擇(或拍攝)照片後導航到相同的控制器並顯示圖像。在控制器中,我將添加以下幾行代碼:

- (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來自委託。它正在工作(當我嘗試在控制器中的底部菜單,而不是現在當我添加到AppDelegate)。在AppDelegate中,呈現視圖控制器存在問題(我知道爲什麼,但我不知道如何繞過這個問題)。我需要讓它在AppDelegate中工作。我不想將其複製到每個控制器並在AppDelegate中更改BottomMenu中的委託。是否有可能做到全球?

+0

創建一個baseViewController並添加所有圖像piking工作,並在所有類中導入baseViewController。可以解決你的問題。 –

回答

1

是的。爲什麼不叫:

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

如果在AppDelegate中沒有navigationController屬性,嘗試其他的方式來獲得當前所呈現的視圖控制器。例如嘗試self.window.rootViewController。你可能應該放棄使用segues。

+1

我正在考慮這兩個解決方案(這一個和Ashish Kakkad評論),我選擇你的。這兩種解決方案都有優點和缺點,在我的項目中,這個方案對我來說有更多優點。謝謝 –

+0

不客氣! – kelin