开发者

presentmodalviewcontroller issue

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.


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.


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;


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];


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];   
}  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜