ViewController에서 TableViewController와 함께 PushViewController
문제
UIViewController가 있으며이 컨트롤러는 NavigationController에 포함되어 있습니다. 이 ViewController에 UitableViewController를 추가합니다. TableView의 셀을 누를 때 PushViewController 메소드를 호출하고 싶습니다.
나는 이것을 시도했다 :
UitableViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstView *myViewController = [[FirstView alloc] init];
[f myViewController];
}
UIViewController (FirstView)
-(void)pushIt
{
SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:sCont animated:YES];
NSLog(@"didSelect"); // is printed
[sCont release];
sCont = nil;
}
그러나 아무 일도 일어나지 않습니다. 나는 nslog ()를 내 pushit 메소드에 넣고 그것을 볼 수 있습니다. 그래서 나는 그것을 밀 수없는 이유를 이해하지 못합니다.
아이디어가 있습니까?
해결책
UIViewController
이름이 지정된 속성이 있습니다 navigationController
a UINavigationController
View Controller에 하나가있는 경우 호출됩니다.
이 속성을 사용하면 뷰 컨트롤러를 테이블 뷰에서 내비게이션 스택으로 푸시 할 수 있습니다. didSelectRowAtIndexPath:
방법.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:sCont animated:YES];
[sCont release];
}
현재 코드가 작동하지 않는 이유는 다음과 같습니다.
- 당신은 이미 테이블 뷰를 하위 뷰로 추가했다고 말했듯이 이미 FirstViewController 인스턴스가 있습니다.
- 사용자가 내비게이션 스택에없는 셀을 탭할 때 FirstViewController의 새 인스턴스를 만들려고 시도하므로 뷰 컨트롤러를 스택으로 밀어 넣으려고 시도합니다.
navigationController
속성은 nil을 반환합니다.
다른 팁
당신은 MyViewController를 할당하고 이니브하지만 내비게이션이나 창이나 다른 것을 푸시하지 않으면 SCONT를 창에 존재하지 않는 MyViewController로 푸시합니다. 먼저 MyViewController를 사용하지 말고 다음으로 SCONT를 밀기 전에 MyViewController를 탐색으로 푸시하십시오.