SettingsViewController

SettingsViewController

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

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处于 Activity 状态)。因此,当您尝试击打视图上的按钮时,没有SettingsViewController可以回调这些事件,因此会导致崩溃。创建属性并对其进行分配后,控制器将继续存在。

关于ios - 删除Subview和View Controller?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22725863/

10-12 06:02