2009-05-25 7 views
1

実験的なUINavigationControllerベースのiPhoneアプリケーションを試していましたが、ユーザーが前のビューに戻ると問題に遭遇しました。同じクラスのUIViewControllerインスタンスでUINavigationControllerを使用し、NIBはありません

単純なアプリケーションはUIViewControllerの新しいインスタンスがプッシュされるUINavigationControllerを使用します。 これらのインスタンスはすべて同じクラス(この例では、MyViewControllerクラスのUIViewControllerのサブクラス)であり、手動で作成されます(NIBを使用しません)。各インスタンスには、UIViewControllerのビューとは別のUITableViewインスタンスが含まれています。

次のtableView:didSelectRowAtIndexPath:メソッドは、MyViewControllerクラスのメソッドです。ユーザは、表のセルを選択したときには、navigationControllerに別のMyViewControllerのインスタンスを作成し、プッシュ:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MyViewController *nextViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; 
    [self.navigationController pushViewController:nextViewController animated:YES]; 
    [nextViewController release]; 
} 

ユーザがビューのシーケンステーブルを含む各一つを介して前方にナビゲートすることができます。この問題は、前の画面に戻るときに発生します。アプリケーションが異常終了し、xcodeがデバッガを起動します。

エラーは、上記のtableView:didSelectRowAtIndexPath:メソッドでMyViewControllerインスタンスを解放しないか、MyViewControllerのdeallocメソッドの 'my​​TableView'インスタンスでdeallocを呼び出さないことで防ぐことができます。 しかし、それは本当の解決策ではありません。私の知る限り、UINavigationControllerはプッシュされたUIViewControllerインスタンスを「所有」しています。このインスタンスは、それを割り当てたクライアントから安全に解放できます。だから、この実験的なアプリケーションで何が間違っているのでしょうか?なぜユーザーが戻るときに終了するのですか?おかげでロブ・ネイピアの問題を指摘して - 固定

問題:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     self.title = @"My Table"; 
     myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     myTableView.delegate = self; 
     myTableView.dataSource = self; 
     self.view = myTableView; 
    } 
    return self; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTable"]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0,0, 300, 50) reuseIdentifier:@"MyTable"]; 
     [cell autorelease]; 
    } 
    cell.text = [NSString stringWithFormat:@"Sample: %d", indexPath.row]; 
    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 3; // always show three sample cells in table 
} 

- (void)dealloc { 
    [myTableView dealloc]; 
    [super dealloc]; 
} 

EDIT:

以下

はMyViewControllerクラスのいくつかの他の方法です。

-loadView方法について地元のUITableViewのインスタンスを使用してビューを設定します:

- (void)loadView { 
    UITableView *myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    myTableView.delegate = self; 
    myTableView.dataSource = self; 
    self.view = myTableView; 
    [myTableView release]; 
} 

答えて

2

あなたは間違った方法でのビューを設定しています。これを-loadViewに設定してください。-initwithNibName:bundle:では設定しないでください。 View Controller Programming Guide例については、「View Controllerの使用」を参照してください。

関連する問題