2013-10-18 5 views
1

UITableViewController에서 다른 컨트롤러로 데이터를 전달하려고합니다. 이것은 초기보기 컨트롤러의 코드입니다.스토리 보드를 사용하여 두 컨트롤러간에 데이터 전달

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:indexPath]; 
[self showList:subject animated:YES]; 
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

- (void)showList:(Subject *)subject animated:(BOOL)animated { 
ListsViewController *lists = [[ListsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
lists.subject = subject; 
NSLog(@"%@", lists.subject); 

[self performSegueWithIdentifier:@"showDetail" sender:self]; 
} 

로그 출력에서 ​​원하는 데이터가 전달되었다고 표시되었습니다. 그러나 내가 단편을 수행하고 ListsViewControllersubject를 기록하면 null이 표시됩니다.

아이디어가 있으십니까?

답변

2

prepareForSegue:sender: 메서드를 덮어 써야합니다. 작동하지 않았다 당신의 코드가 showList:animated: 방법 당신이 ListsViewController 인스턴스를 만든이며, 그것을 subject를 할당하지만,이보기 왜 빠른 수정

- (void)showList:(Subject *)subject animated:(BOOL)animated 
{ 
    [self performSegueWithIdentifier:@"showDetail" sender:subject]; 
} 

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 
     ListsViewController *controller = ([segue.destinationViewController isKindOfClass:[ListsViewController class]]) ? segue.destinationViewController : nil; 
     controller.subject = ([sender isKindOfClass:[Subject class]]) ? subject : nil; 
    } 
} 

이유가 될 것입니다 컨트롤러는 절대로 발표되지 않았다. 대신 performSegueWithIdentifier:sendersubject에 대해 아무것도 모르는 ListsViewController 클래스의 다른 인스턴스를 만듭니다. 따라서 UIStoryboardSegue가 스토리 보드에서 대상보기 컨트롤러를 인스턴스화하고 원하는 방식으로 구성 할 때까지 기다릴 필요가 있습니다.이 방법은 prepareForSegue:sender: 메소드에서 수행 할 수 있습니다.

performSegueWithIdentifier:sender 메서드에서 보낸 사람으로 subject을 사용하는 것이 가장 좋지 않을 수도 있습니다. 발신자가 아니기 때문입니다. :) 내가 할 것이다 당신의 뷰 컨트롤러 클래스의 속성 제목을 작성하고 prepareForSegue:sender:

@interface MyViewController() 

@property (strong, nonatomic) Subject *subject; 

@end 


@implementation MyViewController 

- (void)showList:(Subject *)subject animated:(BOOL)animated 
{ 
    self.subject = subject; 
    [self performSegueWithIdentifier:@"showDetail" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 
     ListsViewController *controller = ([segue.destinationViewController isKindOfClass:[ListsViewController class]]) ? segue.destinationViewController : nil; 
     controller.subject = self.subject; 
    } 
} 

... 
@end 
+0

그것은 SEGUE의 라이프 사이클과 같은 방법을 알고 가치를. 문서는 [여기] (https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html#//apple_ref/doc/uid/TP40007457-CH16-SW1)입니다. –

0

prepareForSegue을 구현 사용하고 좋은이 Methos는

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if([seque.identifier isEqualToString:@"showDetail"]) 
{ 
ListsViewController *lists = seque.destinationViewController; 
lists.subject = subject; 
} 
} 
0

날짜를 통과,하지만 지금은이를 추가해야 :

먼저 대신 :

[self performSegueWithIdentifier:@"showDetail" sender:self]; 

당신이 필요로하는 첫 번째 뷰 컨트롤러 지금

@property (nonatomic, strong) Subject * subjectSegue; 

을 그리고 :

[self performSegueWithIdentifier:@"showDetail" sender:subject]; 

당신의 ListsViewController.h에 속성을 추가

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 
     ListsViewController * lists = (ListsViewController *)[segue destinationViewController]; 
     lists.subjectSegue = sender; 
} 
0

당신은 performSegueWithIdentifier:sender:가 생성하는 이해하는 데 필요한 개체를 보내 뷰 컨트롤러의 새로운 인스턴스 따라서 사용자가 만든 ListsViewController이 화면에 표시되지 않습니다.

당신은`prepareForSegue를 오버라이드 (override) 할 필요가 : 보낸 사람 :

- (void)showList:(Subject *)subject animated:(BOOL)animated 
{ 
    [self performSegueWithIdentifier:@"showDetail" sender:self]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 
     ListsViewController *controller = (ListsViewController *)segue.destinationViewController; 
    controller.subject = self.subject; 
} 
관련 문제