如何在iOS5中关闭presentModalViewController控制器

时间:2012-05-14 14:31:12

标签: iphone

我是iphone开发的新手。我在Xcode4.2和iOS5工作。我用于导航一个视图到另一个:这工作正常..

-(IBAction)viewFullProfile:(id)sender
{
    OperatorFullProfile *operatorFullProfile = [[OperatorFullProfile alloc] initWithNibName:@"OperatorFullProfile" bundle:nil];    
    [self presentModalViewController:operatorFullProfile animated:YES];

}

但是当我解雇OperatorFullProfile时,它没有被驳回。对于解雇代码:

-(IBAction)Cancel:(id)sender
{
  [self.parentViewController dismissModalViewControllerAnimated: YES];
 // [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
 // [self dismissViewControllerAnimated:YES completion:nil ];
}

我正在尝试解决这个问题,请帮忙。

4 个答案:

答案 0 :(得分:1)

只需致电:

[self dismissModalViewControllerAnimated: YES];

应该做的伎俩,不需要获取父视图控制器。

答案 1 :(得分:0)

在您的取消按钮方法的OperatorFullProfile.m文件中

...

-(IBAction)Cancel:(id)sender
{
  [self dismissModalViewControllerAnimated: YES];

}

你当前这个观点将会消失...... :)

答案 2 :(得分:0)

您必须从提供模态视图控制器的dismissModalViewControllerAnimated:调用UIViewController

执行此操作的典型(且设计良好)方法是使用委托。假设模态控制器有一个Done按钮。你在模态视图控制器中声明了@protocol,其方法是didTapDoneButton,并且@property是该协议的一个实例,我们称之为delegate }。

IBAction,请致电

-(IBAction)Cancel:(id)sender
{
    [self.delegate didTapDoneButton];
}

在视图控制器中实现提供模式的协议,并在创建模态控制器时将其委托属性设置为self

-(IBAction)viewFullProfile:(id)sender
{
    OperatorFullProfile *operatorFullProfile = [[OperatorFullProfile alloc] initWithNibName:@"OperatorFullProfile" bundle:nil];    
    operatorFullProfile.delegate = self;
    [self presentModalViewController:operatorFullProfile animated:YES];

}

在该类中,实现协议的方法,按下按钮时将调用该方法:

-(void)didTapDoneButton
{
    [self dismissModalViewControllerAnimated: YES];
}

这样,负责呈现模态控制器的控制器也负责解雇它。

答案 3 :(得分:0)

只需写下:

[self dismissModalViewControllerAnimated:YES];

干杯。