我很难让它工作。我有两个具有关联视图的视图控制器,分别称为DomainSelectionViewController和DomainViewController。我正在阅读有关Apple开发人员网络的教程,其中涉及演示视图控制器。尝试运行时,我收到EXC_BAD_ACCESS信号。
以下是每个文件的相关摘录:
DomainSelectionViewController.h
@class DomainViewController;
@interface DomainSelectionViewController : UIViewController
- (IBAction)domainSelected:(id)sender;
- (IBAction)leaveDomain;
@property (retain) DomainViewController * selectedDomain;
@end
domainSelected:附加到代表域的按钮上。单击它可以成功将界面中的当前视图替换为DomainViewController的笔尖中定义的视图。
DomainSelectionViewController.m
@implementation
- (IBAction)domainSelected:(id)sender {
NSLog(@"Domain Selected...");
selectedDomain = [[DomainViewController alloc] initWithNibName:@"DomainView" bundle:nil];
selectedDomain.domainSelectionContext = self;
[self presentViewController:selectedDomain animated:NO completion:nil];
}
- (IBAction)leaveDomain {
NSLog(@"Leaving Domain...");
NSLog(@"Presented Domain: %@", self.presentedViewController);
//selectedDomain.modalPresentationStyle = UIModalPresentationFullScreen;
[self dismissViewControllerAnimated:NO completion:nil];
}
DomainViewController.h
#import <UIKit/UIKit.h>
#import "DomainSelectionViewController.h"
@class DomainSelectionViewController;
@interface DomainViewController : UIViewController
//@property (nonatomic, assign) DomainSelectionViewController * presentingViewController;
@property (nonatomic, retain) DomainSelectionViewController * domainSelectionContext;
@end
DomainViewController.m
- (IBAction)exit:(id)sender {
NSLog(@"Leaving Domain...");
if(self.presentingViewController) {
NSLog(@" Dismissing View Controller: %@.", self.presentingViewController);
[self.domainSelectionContext leaveDomain];
//[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
return;
}
else {
NSLog(@"Presenting view controller not set.");
}
}
domainview仅包含一个读取“后退”的按钮,并连接到其自己的exit:函数,该函数依次在其委托上调用LeaveDomain。在单击此按钮后,将发生EXC_BAD_ACCESS调用。看看其他类似的帖子,据说EXC_BAD_ACCESS错误通常是由于尝试调用已释放对象而引起的,但是在dismiss调用之前的print语句显示对象仍然存在并且可以引用。我希望有一个比我能看的经验更多的人,并容易地判断出哪里出了问题。
为了完整起见,这是控制台的输出:
Attaching to process 26860.
2012-03-24 19:23:45.601 domaintest[26860:f803] DomainSelectionView Initialized.
2012-03-24 19:23:52.627 domaintest[26860:f803] Domain Selected...
2012-03-24 19:24:14.187 domaintest[26860:f803] Leaving Domain...
2012-03-24 19:24:14.188 domaintest[26860:f803] Dismissing View Controller: <DomainSelectionViewController: 0x688f9a0>.
2012-03-24 19:24:14.188 domaintest[26860:f803] Leaving Domain...
2012-03-24 19:24:14.188 domaintest[26860:f803] Presented Domain: <DomainViewController: 0x6891d90>
Current language: auto; currently objective-c
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
(gdb)
最佳答案
因此,经过反复尝试,这里的问题在视图层次结构中更高。该设计具有一个完全为空的根视图控制器,该控制器用第一个(DomainSelectionViewController)控制器的视图替换了自己的视图。
self.window.rootViewController.view = domainSelectionViewController.view;
最终结果是domainSelectionViewController可以显示domainViewController的视图,但是尝试关闭它会导致EXC_BAD_ACCESS。我仍然不完全确定为什么,但是更改它以使domainSelectionViewController是主视图,或者在ViewDidAppear中让rootViewController存在domainSelectionViewController可以解决此问题。
关于objective-c - 调用dismissViewControllerAnimated:completion:(iOS5)时出现EXC_BAD_ACCESS错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9856684/