【问题标题】:presentmodalviewcontroller issuepresentmodalview 控制器问题
【发布时间】:2011-09-01 12:41:01
【问题描述】:

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 { page *login=[[page alloc]init];
    login.prodid = Prodid;
    login.categid=self.categid;
    UINavigationController *navCtrl= [[UINavigationController alloc] initWithRootViewController:login];
    [self presentModalViewController:navCtrl 动画:YES];
    [登录发布];
    [navCtrl 释放];
    [快速发布];
    }

在我的下一页(C)中有一个取消按钮

-(void) cancel  
{  
    [self dismissModalViewControllerAnimated:YES];  
}  

如果我单击取消按钮应用程序崩溃。我检查 nszombie 并找到过度释放的对象 (Prodid)。 如果我删除 [Prodid 版本] 应用程序可以工作,但在 Prodid 中泄漏。我该如何解决这个问题。

【问题讨论】:

  • Prodidtype 是什么?
  • @EmptyStack Mutablestring 检查我编辑的问题
  • prodid 如何在您的 page 课程中被拒绝?
  • @ Nekto @property (nonatomic, 保留) NSString * prodid;

标签: iphone objective-c memory-leaks


【解决方案1】:
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 的消息不会做任何事情。

【讨论】:

  • 如果您在其他地方正确清理,则不需要 else { Prodid = nil; } 部分。
【解决方案2】:

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

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

[Prodid release];
Prodid = nil;

【讨论】:

  • 你比我早了 11 秒!
  • 大声笑,但你的答案比我的更详细,投给你;)
【解决方案3】:

您应该在调试器中检查您的应用崩溃的代码部分。根据您的描述,我假设您正在尝试将dismissModalViewController 消息发送给navCtl,而不是发送给它的所有者。

[self.parentViewController dismissModalViewControllerAnimated:YES];

【讨论】:

    【解决方案4】:

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

    这样想:呈现一个模态视图控制器提供了一种所有权。您的第二个视图控制器由第一个视图控制器拥有,并且没有任何东西。因此,调用 '[self dismissModalViewControllerAnimatied: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];   
    }  
    

    【讨论】:

    • @mbm30075 我在“UINavigationController”中收到警告,可能无法响应“-cancelButton”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2010-12-09
    • 2011-10-28
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多