繁体   English   中英

删除Subview和View Controller?

[英]Removing Subview and View Controller?

我通过以下方式添加子视图”

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

我的子视图是300 x 360,位于屏幕中央。 当我尝试将其删除(正在发布)时,会出现问题。

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

这是设置视图中关闭按钮的外观:

-(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

最后但并非最不重要的,这是我的情节提要/ xib命令的快照:

在此处输入图片说明

为SettingsViewController创建一个属性,在其中添加并在创建它的位置分配它。

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

注意:此行为的原因是在将ViewController的.view属性添加到您的视图之后,控制器被释放了(只有.view现在还活着)。 因此,当您尝试击打视图上的按钮时,没有SettingsViewController可以回调这些事件,因此会导致崩溃。 创建属性并对其进行分配后,控制器将继续存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM