0
votes

i have the following problem:

I have a storyboard containing three views with UIViewController classes. I do not use seques and apart from initial view (call it View A), all other views are created programmatically with presentViewController method. The problem i have is that when A opens B with presentViewController and then B opens C with presentViewController, when i dissmiss C with dismissViewControllerAnimated (in C there is a Button with Outlet to C UIViewController calling dismissViewControllerAnimated on self), C View disapears, and B appers but only for a 0.1 sec, and then i get C View showing again, and after that the close button wont work anymore.

Any idea what the reason might be?

Best Regards Edin


//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
Any ideas what the problem could be (plz see comments in first answer)? - ededagic
Can you upload a sample project so that I can test this out? - Bot
okay ill do it in next few days, thnx - ededagic

1 Answers

2
votes

You should NEVER have a view dismissing itself. You should always have the parent view dismiss the child.

  • B should dismiss C
  • A should dismiss B

Check http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14 for more information.

enter image description here