Question

I'm adding a subview the following way"

SettingsViewController *SVC = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
[self.parentViewController addChildViewController:SVC];
[self.view addSubview:SVC.view];

My subview is 300 x 360 and is centered on the screen. The problem occurs when I attempt to remove it (Getting released).

When I press the close button in this new view, I get exc bad access. What's the correct way to add my view? I have a SettingsViewController.xib linked up to a .h and .m file.

Here's what my close button in my settings view looks like:

-(IBAction)close:(id)sender {
   [self.view removeFromSuperview];
   [self removeFromParentViewController];
}

But even when I comment out everything inside, just triggering the button crashes the app.

This is what my .h file looks like:

#import <UIKit/UIKit.h>

@interface SettingsViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIView *alertView;
@property (nonatomic, strong) IBOutlet UIButton *closeButton;
@property (nonatomic, strong) IBOutlet UILabel *musicLabel;
@property (nonatomic, strong) IBOutlet UILabel *soundFXLabel;
@property (nonatomic, strong) IBOutlet UILabel *vibrateLabel;
- (IBAction)close:(id)sender;
@end

and my implementation file:

    #import "SettingsViewController.h"

@interface SettingsViewController ()

@end

@implementation SettingsViewController
@synthesize alertView;

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    self.view.alpha = 0;
    self.alertView.layer.cornerRadius = 10.0f;
    self.alertView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.alertView.layer.borderWidth = 4.0f;
    self.closeButton.layer.cornerRadius = 5.0f;
    self.closeButton.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:20];
    self.musicLabel.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:30];
    self.soundFXLabel.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:30];
    self.vibrateLabel.font = [UIFont fontWithName:@"SourceSansPro-Bold" size:30];


    [UIView animateWithDuration:0.3
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.view.alpha = 1;
                     }
                     completion:^(BOOL finished){
                         //Display Players View

                     }];
    }

    - (IBAction)close:(id)sender {
        //[self.view removeFromSuperview];
        //[self removeFromParentViewController];
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    @end

And last but not least, here's a snapshot of my storyboard / xib order:

enter image description here

Was it helpful?

Solution

create a property for your SettingsViewController where you add & assign it where you create it.

// in .h file or private category on top 
    @property (nonatomic, strong) SettingsViewController *settingsController;

// your usual code + assigning controller    
    SettingsViewController *SVC = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
    [self.parentViewController addChildViewController:SVC];
    [self.view addSubview:SVC.view];
    self.settingsController = SVC;

Side Note : the reason for this behavior is after the .view property of the ViewController is added to your view, the controller gets deallocated (only the .view is alive now). So when you try to hit the butons on view there is no SettingsViewController to callback those events, hence crash. When you create the property & assign it, the controller lives.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top