当前模态视图控制器问题
A-->B 子视图(viewcontroller.view)-->Presentmodalviewcontroller(C)
我的第二页:(B) 代码是
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[currentElement release];
currentElement = [elementName copy];
if ([elementName isEqualToString:@"result"] ) {
Prodid = [[NSMutableString alloc] init];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser { 页 *login=[[页分配]init];
login.prodid = Prodid;
login.categid=self.categid;
UINavigationController *navCtrl= [[UINavigationController alloc] initWithRootViewController:login];
[自我呈现ModalViewController:navCtrl动画:是];
[登录发布];
[navCtrl 释放];
[产品发布];
。
在我的下一页(C)中,有一个取消按钮,
-(void) cancel
{
[self dismissModalViewControllerAnimated:YES];
}
如果我单击取消按钮,应用程序崩溃。我检查 nszombie 并找到过度释放的对象(Prodid) 如果我删除 [Prodid release],应用程序可以运行,但 Prodid 中会泄漏。我该如何解决这个问题。
A-->B subview(viewcontroller.view)-->Presentmodalviewcontroller(C)
My second Page:(B) code is
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
[currentElement release];
currentElement = [elementName copy];
if ([elementName isEqualToString:@"result"] ) {
Prodid = [[NSMutableString alloc] init];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
page *login=[[page alloc]init];
login.prodid = Prodid;
login.categid=self.categid;
UINavigationController *navCtrl= [[UINavigationController alloc] initWithRootViewController:login];
[self presentModalViewController:navCtrl animated:YES];
[login release];
[navCtrl release];
[Prodid release];
}
in my next page(C) there is one cancel button
-(void) cancel
{
[self dismissModalViewControllerAnimated:YES];
}
if i click the cancel button the app crash .I check nszombie and find overreleased object (Prodid).
If i remove [Prodid release] the app works but leaks in Prodid.How can i solve this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您并不总是在释放 Prodid 之前分配它。将代码更改为仅在分配时才释放它。也许
这会起作用,因为发送到 nil 的消息不会执行任何操作。
You don't always allocate Prodid before releasing it. Change your code to only release it if you allocate it. Maybe
This will work because messages sent to nil don't do anything.
你的代码很奇怪,如果你的 elementName != @"result" 会发生什么,在这种情况下 Prodid 的值是多少?
我认为你需要在发布后设置 Prodid = nil :
Your code is weird, what's happen if your elementName != @"result", what is the value of Prodid in this case ?
I think you need to set Prodid = nil after release it :
您应该在调试器中检查应用程序崩溃的代码部分。根据您的描述,我假设您正在尝试向 navCtl,而不是其所有者发送解雇ModalViewController 消息。
You should ckeck in debugger the part of code, where your app had crashed. From your description I assume, that you are trying to send dismissModalViewController message to navCtl, not to its owner.
如果您检查 iOS 文档,您会发现模态视图控制器自行解散(如果可能的话)是一种不好的形式。正确的形式是让您的第一个视图控制器执行关闭。
可以这样想:呈现模式视图控制器提供了一种所有权。您的第二个视图控制器由第一个视图控制器拥有,并且不拥有任何内容。因此,调用“[selfmissModalViewControllerAnimatied:YES]”失败,因为第二个视图控制器没有模态视图控制器。
通常,在这种情况下,我在“基本”视图控制器和模式视图控制器之间建立了某种委托关系。在设置时,您还可以从“基本”视图控制器将目标添加到取消按钮。
也许是这样的:
If you check the iOS documentation, you'll see that it's bad form for a modal view controller to dismiss itself (when it's possible at all). The proper form is for your first view controller to perform the dismiss.
Think of it this way: Presenting a modal view controller gives a sort of ownership. Your second view controller is OWNED by the first view controller and OWNS nothing. Therefore, calling '[self dismissModalViewControllerAnimatied:YES]' fails because the second view controller DOES NOT HAVE a modal view controller.
Typically, in this situation, I've set up some sort of a delegate relationship between the "base" view controller and the modal one. You could also add a target to the cancel button from the "base" view controller when setting it up.
Maybe like this: