我添加一個子視圖通過以下方式」刪除子視圖和視圖控制器?
SettingsViewController *SVC = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:[NSBundle mainBundle]];
[self.parentViewController addChildViewController:SVC];
[self.view addSubview:SVC.view];
我的子視圖是300×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秩序的快照:
你的關閉和/或刪除代碼是什麼樣的? –
你確定你的訪問不良是從一個連接到變量名稱或刪除的變量的出口發出的嗎? –
@MichaelDautermann剛剛更新我的回答 – KingPolygon