消息发送到已释放的实例!找不到错误
好吧,我从 2 小时开始就在寻找这个错误,但我就是无法弄清楚,请帮助我。 我有以下情况我有 2 个视图控制器。 一个将另一个呈现为类似的模态视图。
SearchViewController *searchViewController = [[SearchViewController alloc]init];
[searchViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
searchViewController.delegate = self;
searchViewController.senderTag = [sender tag];
[self presentModalViewController:searchViewController animated:YES];
[searchViewController release];
在我的 searchviewcontroller 中,我在 .m 文件中的 .h 文件中执行此操作
BSKmlResult *selectedAirport;
@property (nonatomic, retain) BSKmlResult *selectedAirport;
,我合成它,然后像这样设置它
selectedAirport = [self.airportList objectAtIndex:indexPath.row];
,然后
- (void)dealloc {
[selectedAirport release];
[super dealloc];
}
在我的 SearchViewController 的委托方法中释放它,该方法在第一个中实现 viewcontroller,我还展示了 SearchViewController 我有以下内容,
if (controller.selectedAirport) {
if (departureAirport) {
[departureAirport release];
}
departureAirport = [controller.selectedAirport copy];
}
[self dismissModalViewControllerAnimated:YES];
我缩小了错误发生的位置,它是在我的 SearchViewController 的 dealloc 中 【选定机场发布】;
但我不知道我的错误在哪里 请帮忙
alright I am looking for this error since 2 hours and I just cant figure it out please help me.
I have the following situation I have 2 viewcontroller.
one presents the other one as modalview like that.
SearchViewController *searchViewController = [[SearchViewController alloc]init];
[searchViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
searchViewController.delegate = self;
searchViewController.senderTag = [sender tag];
[self presentModalViewController:searchViewController animated:YES];
[searchViewController release];
in my searchviewcontroller I do this in the .h file
BSKmlResult *selectedAirport;
@property (nonatomic, retain) BSKmlResult *selectedAirport;
in the .m file i synthesize it and then set it like that
selectedAirport = [self.airportList objectAtIndex:indexPath.row];
and then release it here
- (void)dealloc {
[selectedAirport release];
[super dealloc];
}
in the delegate methode of my SearchViewController which is implemented in the first
viewcontroller where I also present the SearchViewController
i have the following
if (controller.selectedAirport) {
if (departureAirport) {
[departureAirport release];
}
departureAirport = [controller.selectedAirport copy];
}
[self dismissModalViewControllerAnimated:YES];
I narrowed down where the error happens it is in the dealloc of my SearchViewController
[selectedAirport release];
but I cant figure out where my mistake is
please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会在此处保留选定的机场。
将其更改为
既然你找不到它,可能你不知道这一点...
如果你不通过 self.memberVariable 访问成员变量,那么你就没有访问它的属性。因此,它没有被保留。
当然你也可以保留它,说
但是你的财产有什么用呢……
You arent retaining selectedAirport here.
Change it to
Since you couldnt find it out, probably you dont know this...
If you dont access member variables by self.memberVariable, you are not accessing its property. Thus, it was not getting retained.
Ofcourse you can also retain it by saying
But whats the use of your property then...
你需要使用自我。通过综合方法运行它,以获得保留。
You need to use self. to run it through the synthesized method, to get the retain.
我知道这篇文章已经很老了,但只是想添加一些有用的内容。在上述情况下,成员变量名称和属性名称相同,因此您仍然可能会错误地设置成员变量的值,而不是使用将隐式调用保留的属性来访问它。因此,确保始终使用 self.selectedAirport 的最佳方法是将成员变量命名为与属性不同的名称。
例如,在 .h 文件中,您可以有以下实现:
然后将其封装在如下所示的属性中
,并在 .m 实现文件中按如下所示综合它:
通过执行上述操作,如果您尝试像下面一样访问它,
那么它将导致错误,系统将提示您使用 self.selectedAirport。
同样在这种情况下,您的 dealloc 方法可以有
或者
I know this post is quite old but just wanted to add something useful to it. In the above case the member variable name and property name are identical so you may still by mistake set the value of member variable instead accessing it using property that will call retain implicitly. Hence the best way to make sure you always use self.selectedAirport is to name the member variable something different than your property.
For example, in .h file you can have below implementation:
then encapsulate it inside a property like below
and in .m implementation file synthesize it like below:
By doing above, if you try to access it like below
then it would result in an error and you will be prompted to use self.selectedAirport.
Also in this case your dealloc method can have either
or