2010-09-16 69 views
1

當模態視圖完成解除時有沒有辦法調用代碼?UIView通知當模式UIImagePickerController被解僱?

編輯:

對不起,我之前沒有澄清。我試圖解僱一個UIImagePickerController,然後顯示一個MFMailComposeViewController並將圖像數據附加到電子郵件中。當我嘗試調用

[self presentModalViewController: mailController]

之後

[self dismissModalViewController];

我得到的錯誤和這樣的。

+0

看到我的答案更新。 – 2010-09-17 17:33:34

回答

5

您可以使用模式視圖的委託模式來通知完成後呈現的模式。

MyModalViewController.h:

@protocol MyModalViewControllerDelegate; 

@interface MyModalViewController : UIViewController 
{ 
    id<MyModalViewControllerDelegate> delegate; 
} 

@property (nonatomic, assign) id<MyModalViewControllerDelegate> delegate; 

@end 


@protocol MyModalViewControllerDelegate 
- (void)myModalViewControllerFinished:(MyModalViewController*)myModalViewController; 
@end 

MyModalViewController.m:

@synthesize delegate; 

// Call this method when the modal view is finished 
- (void)dismissSelf 
{ 
    [delegate myModalViewControllerFinished:self]; 
} 

ParentViewController.h:

#import "MyModalViewController.h" 

@interface ParentViewController : UIViewController <MyModalViewControllerDelegate> 
{ 
} 

ParentViewController.m:

- (void)presentMyModalViewController 
{ 
    MyModalViewController* myModalViewController = [[MyModalViewController alloc] initWithNibName:@"MyModalView" bundle:nil]; 
    myModalViewController.delegate = self; 
    [self presentModalViewController:myModalViewController animated:YES]; 
    [myModalViewController release]; 
} 

- (void)myModalViewControllerFinished:(MyModalViewController*)myModalViewController 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

編輯:

我沒有用過UIImagePickerController,但在看文檔,它看起來像你已經有最適合你做的代碼,因爲沒有一個現有的UIImagePickerControllerDelegate類,有三種不同的「解僱「委託回調(儘管一個已被棄用)。所以你應該讓你的ParentViewController類(不管那是什麼)實現UIImagePickerControllerDelegate模式,然後實現這些方法。雖然每種方法都會做不同的事情(因爲您必須在用戶實際選擇圖片或取消圖片時處理),但每種方法最後都會做同樣的事情:致電dismissModalViewControllerAnimated:關閉選取器。

+0

我可以在UIImagePickerController上使用委託模式和自定義委託嗎? – Moshe 2010-09-17 14:34:41

0

你必須解僱modalViewController莫名其妙?任一個UIButton,或通過代碼:

- (void)dismissModalViewControllerAnimated:(BOOL)animated 

在IBAction爲(例如代表)用於UIButton的或在上述方法,打電話給你想要的任何代碼。

+0

你是說重寫' - (void)dismissModalViewControllerAnimated:(BOOL)animated'?我不認爲這會奏效,請參閱我的編輯。 – Moshe 2010-09-17 14:35:18

0

我不認爲有一個具體的通知,但可以訂閱,知道何時解僱動畫完成,但是。您可以在呈現模態視圖的視圖控制器中實現viewDidAppear:。這是我所做的,當我使用(與UIImagePickerController非常相似)ABPeoplePickerNavigationController。

在從人回調選擇器,我記得在選擇器拍了拍一個實例變量的人,像這樣的:

- (void)callbackFromModalView:(id)dataFromModalView { 
    // remember dataFromModalView as I need it when dismissed 
    self.dataFromModalView = dataFromModalView; 

    // now initiate dismissal 
    [self dismissModalViewControllerAnimated:YES]; 
} 

那麼,在您的視圖控制器,實現這一點:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    if (self.dataFromModalView) { 
     //...present now view here 

     // don't forget to reset this one 
     self.dataFromModalView = nil; 
    } 
} 

實際上,您正在使用viewWillAppear:dataFromModalView屬性的組合作爲「關於已取消模態視圖的通知」。