1

私はかなり珍しい状況にあり、メモリ管理を正しく理解していません。

メッセージを表示するためのUITableViewControllerがあり、UINavigationControllerを作成し、そのビューを表示するために現在のビューのサブビューとして追加します。私が抱えている問題は、UINavigationControllerをリリースしないために、Xcodeがメモリリークの可能性があることを報告していることです(同意します)が、以下のコードのようにリリースすると、クリックして戻ったときにアプリがクラッシュしますテーブルビュー。

UITableViewControllerの保持プロパティを使用して、現在のUINavigationControllerを追跡し、保持カウントを管理しましたが、明らかにここに何かが欠けています。

注:クラッシュは、メッセージ-[UILayoutContainerView removeFromSuperview:]:認識されないセレクターがインスタンス0x5537db0に送信された状態で[戻る]ボタンをクリックすると発生します。

また、コードの[nc release]行を削除すると、正常に機能することにも注意してください。

これが私のコードです:

@property(nonatomic, retain) UINavigationController *currentNavigationController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nc = [[UINavigationController alloc] init];

    CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
    [[nc view] setFrame:ncFrame];

    // I created a CurrentNavigationController property to 
    // manage the retain counts for me
    [self setCurrentNavigationController:nc]; 

    [[self view] addSubview:[nc view]];
    [nc pushViewController:messageDetailViewController animated:YES];


    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[nc view] action:@selector(removeFromSuperview:)];


    nc.navigationBar.topItem.leftBarButtonItem = bbi;
    [bbi release];

    [nc release];
}
4

1 に答える 1

1

作成した UINavigationController 「nc」は、このメソッド内でのみ使用できます。このメソッドの後にはどこにも保存されません (解放するため)。したがって、navigationController のビューをクラス ビューにサブビューとして追加してから、navigationController を削除します。それは間違っている。ビューと viewController がそれらの navigationController を参照しようとすると (存在しない場合)、アプリがクラッシュします。

didSelectRowForIndexPath メソッドのコードは次のとおりです。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

    UINavigationController *nc = [[UINavigationController alloc] init];

    CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
    [[nc view] setFrame:ncFrame];

    [self setCurrentNavigationController:nc]; 
    [nc release];

    [[self view] addSubview:[self.currentNavigationController view]];

    UIViewController *viewCont = [[UIViewController alloc] init];
    [viewCont.view setBackgroundColor:[UIColor greenColor]];

    [nc pushViewController:viewCont animated:YES];

    NSLog(@"CLASS %@",[[self.currentNavigationController view]class]);

    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[self.currentNavigationController view] action:@selector(removeFromSuperview)];


    self.currentNavigationController.navigationBar.topItem.leftBarButtonItem = bbi;
    [bbi release];
}

removeFromSuperview メソッドのセレクターの末尾に「:」を含めないでください。引数はありません:)

于 2012-09-13T08:37:05.997 に答える