0

我有以下问题:

我有一个故事板,其中包含三个带有 UIViewController 类的视图。我不使用序列,除了初始视图(称为视图 A)之外,所有其他视图都是使用 presentViewController 方法以编程方式创建的。我遇到的问题是,当A用presentViewController打开B,然后B用presentViewController打开C时,当我用dismissViewControllerAnimated解除C时(在C中有一个带有Outlet到C UIViewController的按钮,在self上调用dismissViewControllerAnimated),C View消失,B出现但仅持续 0.1 秒,然后我再次显示 C 视图,之后关闭按钮将不再起作用。

知道可能是什么原因吗?

最好的问候爱丁


//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
4

1 回答 1

2

你永远不应该有一个自我解散的观点。您应该始终让父视图解雇孩子。

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

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

在此处输入图像描述

于 2012-08-22T16:42:54.400 回答