2013-09-26 111 views
1

對我來說這看起來很簡單,但我無法理解我正在犯的錯誤。我必須在iPad應用程序的點擊行中打開彈出窗口。我做了以下代碼:在打開popviewcontroller時出錯

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil]; 
    vc.preferredContentSize = CGSizeMake(500,500); 
    vc.view.frame = CGRectMake(0, 0, 500, 500); 

    UIPopoverController *healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 

當最後一行執行時,應用程序崩潰。我在谷歌搜索了很多頁面,但我無法找到它的原因。我沒有得到任何特定的錯誤,只有主文件中的線程1錯誤。 有人可以幫助我嗎。這讓我感到沮喪。順便說一句我正在使用iOS7。

問候
潘卡

+0

可以打印錯誤? –

+1

問題在於不保留彈出窗口。按@null建議保留popover。 – 2013-09-26 08:24:36

回答

1

嘗試添加您的healthPopOverit因爲UIPopoverControllers必須在一個實例變量保持你的類的成員。

在您的m把它定義爲一個屬性:

UIPopoverController *healthPopOver; 

和改變:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    popViewController *vc = [[popViewController alloc] initWithNibName:@"popViewController" bundle:nil]; 
    vc.preferredContentSize = CGSizeMake(500,500); 
    vc.view.frame = CGRectMake(0, 0, 500, 500); 

    self.healthPopOver = [[UIPopoverController alloc] initWithContentViewController:vc]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [healthPopOver presentPopoverFromRect:cell.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 
+0

非常感謝它的工作......雖然這聽起來對我來說很奇怪。你知道它這樣做的原因嗎? – pankaj

+1

@ pankaj我猜你必須使用ARC。所以你的局部變量'healthPopOver'將在'tableView:didSelectRowAtIndexPath:'函數結束後由ARC發佈。現金可能發生在這一點上。 – SFeng

+1

'UIPopoverController'沒有超級視圖,除非你不保留它,否則它不會被任何人保留,所以通過執行'UIPopoverController * healthPopOver = [[UIPopoverController alloc] init ...'變量變成了的範圍和對象得到釋放,因爲它沒有所有者了,這就是爲什麼你需要保留它。 – null