2010-01-31 4 views
1

내 루트보기에 테이블보기의 세 섹션이 포함되어 있습니다. 선택한 섹션에 따라 해당 뷰 컨트롤러가 뷰 스택에 팝업됩니다. 다음 didSelectRowAtIndexPath 메서드는 내 코드에서 제공하는 것으로 예상대로 작동하지만 올바른/가장 우아한 방법인지 궁금합니다. 저는 Objective-C 신참이기 때문에 viewController를 처음에는 무효로해야하는지 잘 모르겠습니다.항목 선택에 따라 UIViewController 개체를 동적으로로드하는 가장 좋은 방법입니까?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIViewController *viewController = nil; 

    if (indexPath.section == 0) { 
     viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
    } 
    else if (indexPath.section == 1) { 
     viewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    } 
    else if (indexPath.section == 2) { 
     viewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; 
    } 

    [self.navigationController pushViewController:viewController animated:YES]; 
    [viewController release]; 
} 

답변

2

이 작업은 swtch() 문에서 수행 할 수 있지만, 그렇지 않은 경우에는 좋습니다. 다른 변경 사항은 푸시하기 전에 viewController를 확인하는 것입니다.

if (viewController != nil) [self.navigationController pushViewController... 
관련 문제