簡體   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