消息发送到已释放的实例!找不到错误

发布于 2024-11-08 12:39:52 字数 1322 浏览 2 评论 0原文

好吧,我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空名 2024-11-15 12:39:52
selectedAirport = [self.airportList objectAtIndex:indexPath.row];

您不会在此处保留选定的机场。

将其更改为

self.selectedAirport = [self.airportList objectAtIndex:indexPath.row];

既然你找不到它,可能你不知道这一点...

如果你不通过 self.memberVariable 访问成员变量,那么你就没有访问它的属性。因此,它没有被保留。

当然你也可以保留它,说

selectedAirport = [[self.airportList objectAtIndex:indexPath.row] retain];

但是你的财产有什么用呢……

selectedAirport = [self.airportList objectAtIndex:indexPath.row];

You arent retaining selectedAirport here.

Change it to

self.selectedAirport = [self.airportList objectAtIndex:indexPath.row];

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

selectedAirport = [[self.airportList objectAtIndex:indexPath.row] retain];

But whats the use of your property then...

满天都是小星星 2024-11-15 12:39:52

你需要使用自我。通过综合方法运行它,以获得保留。

 self.selectedAirport = [self.airportList objectAtIndex:indexPath.row];

You need to use self. to run it through the synthesized method, to get the retain.

 self.selectedAirport = [self.airportList objectAtIndex:indexPath.row];
乜一 2024-11-15 12:39:52

我知道这篇文章已经很老了,但只是想添加一些有用的内容。在上述情况下,成员变量名称和属性名称相同,因此您仍然可能会错误地设置成员变量的值,而不是使用将隐式调用保留的属性来访问它。因此,确保始终使用 self.selectedAirport 的最佳方法是将成员变量命名为与属性不同的名称。

例如,在 .h 文件中,您可以有以下实现:

NSString *_selectedAirport;

然后将其封装在如下所示的属性中

@property(nonatomic,retain) NSString *selectedAirport;

,并在 .m 实现文件中按如下所示综合它:

@synthesize selectedAirport = _selectedAirport;

通过执行上述操作,如果您尝试像下面一样访问它,

selectedAirport = [self.airportList objectAtIndex:indexPath.row];

那么它将导致错误,系统将提示您使用 self.selectedAirport。

同样在这种情况下,您的 dealloc 方法可以有

self.selectedAirport = nil;

或者

[_selectedAirport release];

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:

NSString *_selectedAirport;

then encapsulate it inside a property like below

@property(nonatomic,retain) NSString *selectedAirport;

and in .m implementation file synthesize it like below:

@synthesize selectedAirport = _selectedAirport;

By doing above, if you try to access it like below

selectedAirport = [self.airportList objectAtIndex:indexPath.row];

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

self.selectedAirport = nil;

or

[_selectedAirport release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文