Rams
Rams

Reputation: 1751

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

        }  
}  

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.

Upvotes: 0

Views: 449

Answers (4)

mbm29414
mbm29414

Reputation: 11598

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

Upvotes: 0

Morion
Morion

Reputation: 10860

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

Upvotes: 0

N_A
N_A

Reputation: 19897

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.

Upvotes: 1

Johnmph
Johnmph

Reputation: 3391

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;

Upvotes: 0

Related Questions