【问题标题】:UIView notification when modal UIImagePickerController is dismissed?模态 UIImagePickerController 被解除时的 UIView 通知?
【发布时间】:2011-04-13 10:29:15
【问题描述】:

有没有办法在模态视图完成关闭时调用代码?

编辑:

对不起,我之前没有澄清。我正在尝试关闭 UIImagePickerController,然后显示 MFMailComposeViewController 并将图像数据附加到电子邮件中。当我尝试打电话时

[self presentModalViewController: mailController]

紧接着

[self dismissModalViewController];

我收到错误等。

【问题讨论】:

    标签: iphone uiviewcontroller ios modal-dialog


    【解决方案1】:

    您使用模式视图的委托模式来通知呈现它的人何时完成。

    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: 来关闭选择器。

    【讨论】:

    • 我可以在 UIImagePickerController 上将委托模式与自定义委托一起使用吗?
    【解决方案2】:

    您必须以某种方式关闭 modalViewController 对吗? UIButton 或代码:

    - (void)dismissModalViewControllerAnimated:(BOOL)animated
    

    在 UIButton 的 IBAction(例如委托)或上述方法中,调用您想要的任何代码。

    【讨论】:

    • 你是说覆盖- (void)dismissModalViewControllerAnimated:(BOOL)animated?我认为这行不通,请参阅我的编辑。
    【解决方案3】:

    我认为目前还没有可以订阅的特定通知,以了解关闭动画何时完成,...但是。您可以在呈现模态视图的视图控制器中实现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 属性的组合作为“关于模式视图已关闭的通知”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      相关资源
      最近更新 更多