当前模态视图控制器问题

发布于 2024-12-02 08:46:14 字数 1107 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

漆黑的白昼 2024-12-09 08:46:14
if ([elementName isEqualToString:@"result"] ) {  

    Prodid = [[NSMutableString alloc] init];  

        }  
} 
[Prodid release];

您并不总是在释放 Prodid 之前分配它。将代码更改为仅在分配时才释放它。也许

if ([elementName isEqualToString:@"result"] ) {  

    Prodid = [[NSMutableString alloc] init];  

}
else
{
    Prodid = nil;
}
[Prodid release];
Prodid = nil;

这会起作用,因为发送到 nil 的消息不会执行任何操作。

if ([elementName isEqualToString:@"result"] ) {  

    Prodid = [[NSMutableString alloc] init];  

        }  
} 
[Prodid release];

You don't always allocate Prodid before releasing it. Change your code to only release it if you allocate it. Maybe

if ([elementName isEqualToString:@"result"] ) {  

    Prodid = [[NSMutableString alloc] init];  

}
else
{
    Prodid = nil;
}
[Prodid release];
Prodid = nil;

This will work because messages sent to nil don't do anything.

黑凤梨 2024-12-09 08:46:14

你的代码很奇怪,如果你的 elementName != @"result" 会发生什么,在这种情况下 Prodid 的值是多少?

我认为你需要在发布后设置 Prodid = nil :

[Prodid release];
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 :

[Prodid release];
Prodid = nil;
℉絮湮 2024-12-09 08:46:14

您应该在调试器中检查应用程序崩溃的代码部分。根据您的描述,我假设您正在尝试向 navCtl,而不是其所有者发送解雇ModalViewController 消息。

[self.parentViewController dismissModalViewControllerAnimated:YES];

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.

[self.parentViewController dismissModalViewControllerAnimated:YES];
裸钻 2024-12-09 08:46:14

如果您检查 iOS 文档,您会发现模态视图控制器自行解散(如果可能的话)是一种不好的形式。正确的形式是让您的第一个视图控制器执行关闭。

可以这样想:呈现模式视图控制器提供了一种所有权。您的第二个视图控制器由第一个视图控制器拥有,并且不拥有任何内容。因此,调用“[selfmissModalViewControllerAnimatied:YES]”失败,因为第二个视图控制器没有模态视图控制器。

通常,在这种情况下,我在“基本”视图控制器和模式视图控制器之间建立了某种委托关系。在设置时,您还可以从“基本”视图控制器将目标添加到取消按钮。

也许是这样的:

- (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];   
    }   
    page *login=[[page alloc]init];   
    login.prodid = Prodid;   
    login.categid=self.categid;   
    UINavigationController *navCtrl= [[UINavigationController alloc] initWithRootViewController:login];   
    [[login cancelButton] addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];

    [self presentModalViewController:navCtrl animated:YES];   
    [login release];   
    [navCtrl release];    
}

-(void) dealloc   
{    
    [Prodid release];   
}   

// Put this method in the "base" view controller, NOT the modal one
-(void) cancel   
{   
    [self dismissModalViewControllerAnimated: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:

- (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];   
    }   
    page *login=[[page alloc]init];   
    login.prodid = Prodid;   
    login.categid=self.categid;   
    UINavigationController *navCtrl= [[UINavigationController alloc] initWithRootViewController:login];   
    [[login cancelButton] addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];

    [self presentModalViewController:navCtrl animated:YES];   
    [login release];   
    [navCtrl release];    
}

-(void) dealloc   
{    
    [Prodid release];   
}   

// Put this method in the "base" view controller, NOT the modal one
-(void) cancel   
{   
    [self dismissModalViewControllerAnimated:YES];   
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文