2011-09-01 66 views
0

A - >乙子視圖(viewcontroller.view) - > Presentmodalviewcontroller(C)presentmodalviewcontroller問題

我的第二頁:(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]; 

     } 
} 
  • (無效)parserDidEndDocument: (NSXMLParser *)解析器頁面* login = [[page alloc] init];
    login.prodid = Prodid;
    login.categid = self.categid;
    UINavigationController * navCtrl = [[UINavigationController alloc] initWithRootViewController:login];
    [self presentModalViewController:navCtrl animated:YES];
    [登錄發佈];
    [navCtrl release];
    [Prodid release];
    }

在我的下一個頁面(C)有一個取消按鈕

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

如果我點擊取消按鈕的應用程序崩潰。我檢查nszombie找到overreleased對象(到產品編號)。 如果我刪除[Prodid發佈]該應用程序的作品,但泄露Prodid.How我可以解決這個問題。

+0

Prodid的''類型是什麼? – EmptyStack

+0

@EmptyStack Mutablestring檢查我編輯的問題 – Rams

+0

你的'page'類中的'prodid'如何被拒絕? – Nekto

回答

0

您應該在調試器中查看應用程序崩潰的代碼部分。根據你的描述,我假設你試圖將dismissModalViewController消息發送給navCtl,而不是它的主人。

[self.parentViewController dismissModalViewControllerAnimated:YES]; 
+0

如何發送到owner.check我編輯的問題 – Rams

+0

我已經添加了代碼到我的文章。 – Morion

0

如果您檢查iOS文檔,您會發現模式視圖控制器無法自行解散(可能時)。適當的形式是您的第一個視圖控制器執行解僱。

想想這樣:呈現一個模態視圖控制器給出了一種所有權。你的第二個視圖控制器是由第一個視圖控制器OWNED和OWNS什麼都沒有。因此,調用'[self dismissModalViewControllerAnymatied: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]; 
} 
+0

@ mbm30075我在'UINavigationController'中收到警告可能不會響應'-cancelButton' – Rams

+0

@Rams:更新了代碼以修復我的錯誤。 – mbm29414

0

你的代碼是奇怪,什麼發生,如果您的ElementName = @「結果」,是什麼的產品編號在這種情況下的價值?

我認爲你需要設置到產品編號=零釋放後:

[Prodid release]; 
Prodid = nil; 
+0

你在11秒內擊敗了我! – mydogisbox

+0

大聲笑,但你的回答比我的更詳細,爲你投票;) – Johnmph

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

    Prodid = [[NSMutableString alloc] init]; 

     } 
} 
[Prodid release]; 

你並不總是釋放之前分配到產品編號。如果您分配它,請將您的代碼更改爲僅釋放它。也許

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

    Prodid = [[NSMutableString alloc] init]; 

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

這將工作,因爲發送到零的消息不會做任何事情。

+0

'else {Prodid = nil;如果你在其他地方進行了適當的清理,則不需要這部分。 – mydogisbox

相關問題