删除子视图和视图控制器?

时间:2014-03-29 01:30:18

标签: ios objective-c uiview subview

我按以下方式添加子视图“

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

我的子视图是300 x 360,并以屏幕为中心。当我尝试将其删除(获释)时会出现问题。

当我在这个新视图中按下关闭按钮时,我得到了很糟糕的访问权限。添加视图的正确方法是什么?我有一个与.h和.m文件链接的SettingsViewController.xib。

以下是我的设置视图中的关闭按钮:

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

但即使我在内部注释掉所有内容,只需触发按钮就会崩溃应用程序。

这是我的.h文件的样子:

#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

和我的实施文件:

    #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

最后但并非最不重要的是,这是我的storyboard / xib顺序的快照:

enter image description here

1 个答案:

答案 0 :(得分:3)

为您的SettingsViewController创建一个属性,您可以在其中添加&amp;在创建它的地方分配它。

// 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注意:此行为的原因是在ViewController的.view属性添加到视图后,控制器被取消分配(现在只有.view处于活动状态)。因此,当您尝试在视图上击中按钮时,没有SettingsViewController来回调这些事件,因此崩溃。当您创建属性&amp;分配它,控制器生活。