堆栈中的iOS模态视图被忽略

时间:2012-08-22 16:31:18

标签: ios views stack modal-dialog

我有以下问题:

我有一个包含UIViewController类的三个视图的故事板。我不使用seques并且除了初始视图(称之为View A)之外,所有其他视图都是使用presentViewController方法以编程方式创建的。我遇到的问题是当A用presentViewController打开B然后B用presentViewController打开C时,当我用dismissViewControllerAnimated解析C时(在C中有一个带有Outlet的按钮到C UIViewController调用dismissViewControllerAnimated on self),C View disapears,和只有0.1秒,然后我再次显示C View,然后关闭按钮不再有效。

知道原因可能是什么?

最诚挚的问候爱丁


//delegate definition used between controller A/B and B/C
@protocol ParentControllerDelegate <NSObject>

//called as delegate method from B on A and from C on B to dismiss B from A and C from B
- (void)dismissView:(UIViewController*)controller;

@end


// MainMenuViewController.h which is controller A
@interface MainMenuViewController : UIViewController <ParentControllerDelegate>

//Controller B property
@property (strong, nonatomic) ChooseLevelViewController *chooseLevelViewController;
//button to open controller B
@property (weak, nonatomic) IBOutlet UIButton *chooseLevelBtn;

@end

//MainMenuViewController.m - Controller A
@implementation MainMenuViewController

//called to present chooseLevelViewController which is controller B
- (IBAction)chooseLevelPressed:(id)sender
{
    if(self.chooseLevelViewController == nil)
    {
        self.chooseLevelViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChooseLevelView"];
        self.chooseLevelViewController.parentControllerDelegate = self;
    }
    [self presentViewController:self.chooseLevelViewController animated:YES completion:nil];
}

//called from controller B over delegate mechanism to dismiss B
- (void)dismissView
{
    [self.chooseLevelViewController dismissViewControllerAnimated:YES completion:nil];
}
@end

// ChooseLevelViewController.h which is controller B
@interface ChooseLevelViewController : UIViewController <ParentControllerDelegate>

//Controller A as delegate
@property (assign, nonatomic) id <ParentControllerDelegate> parentControllerDelegate;
//Controller C property
@property (strong, nonatomic) ChoosePlayerViewController *choosePlayerViewController;
//button to dismiss B over delegate A
@property (weak, nonatomic) IBOutlet UIButton *backMainBtn;
//button to open C
@property (weak, nonatomic) IBOutlet UIButton *choosePlayerBtn;

@end

//MainMenuViewController.m - controller B
@implementation ChooseLevelViewController

//calling controller A as delegate to dismiss B
- (IBAction)backMainBtnPressed:(id)sender
{
    [self.parentControllerDelegate dismissView];
}

//presenting choosePlayerViewController which is controller C
- (IBAction)choosePlayerBtnPressed:(id)sender
{
    if(self.choosePlayerViewController == nil)
    {
        self.choosePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChoosePlayerView"];
        self.choosePlayerViewController.parentControllerDelegate = self;
    }
    [self presentViewController:self.choosePlayerViewController animated:YES completion:nil];
}


//called from controller C over delegate mechanism to dismiss C
- (void)dismissView:(UIViewController*)controller
{
    [self.choosePlayerViewController dismissViewControllerAnimated:YES completion:nil];
}
@end

//ChoosePlayerViewController.h which is controller C
@interface ChoosePlayerViewController : UIViewController

//Controller B as delegate
@property (assign, nonatomic) id <ParentControllerDelegate> parentControllerDelegate;
//button to dismiss C over delegate B
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@end

@implementation ChoosePlayerViewController

//calling controller B as delegate to dismiss C
- (IBAction)closeBtnPressed:(id)sender
{
    [self.parentControllerDelegate dismissView];
}
@end

1 个答案:

答案 0 :(得分:2)

你永远不应该有自己的观点。你应该总是让父视图解雇孩子。

  • B应该解雇C
  • A应该解雇B

检查http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14 了解更多信息。

enter image description here