2012-01-20 3 views
0

테이블보기가 있으며 특정 행을 두드리면 상세보기가 표시됩니다. 그러나 세부보기에 어떤 행이 두드려 졌는지 어떻게 알 수 있습니까? 예를 들어, 이름을 표시하는 MainController가 있습니다. "Johnny"를 탭하면 다음 화면에는 "Johnny"라는 문자열이있는 레이블이 표시됩니다. 내가 어떻게 그럴 수 있니?테이블 뷰 컨트롤러는 어떻게 세부 뷰와 통신합니까?

[편집 - 추가 된 코드]

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ArticleView *article = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleView"]; 
    [article.myLabel setText:@"random"]; 
    [self.navigationController pushViewController:article animated:YES]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [info objectForKey:@"username"]; 
    cell.textLabel.backgroundColor = nil; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.detailTextLabel.backgroundColor = nil; 
    cell.detailTextLabel.textColor = [UIColor whiteColor]; 
    cell.detailTextLabel.text = [info objectForKey:@"user_pic"]; 

    // Configure the cell... 

    return cell; 
} 

ArticleView.h

@interface ArticleView : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *myLabel;  
@end 

ArticleView.m

->의 합성 속성

당신은 할 수
+0

iOS 용, 코코아 터치입니까? 또는 Mac, 코코아? – vikingosegundo

+0

iOS 용입니다 ... –

+0

다음 번 태그를 올바르게 입력하십시오. Xcode는 (고급) 텍스트 편집기이기 때문에이 질문에 중요하지 않습니다. – vikingosegundo

답변

2

페이지 엉덩이 물건 (즉, String Jonny)를 속성으로 상세보기 컨트롤러에 추가합니다. 당신이 indexPath 매개 변수에서 선택하고 있으며를 통과 할 수있는 행 알 수 있도록있는 tableview 컨트롤러 테이블 뷰 대리인이 방법 tableView:didSelectRowAtIndexPath:있다

-(void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath)indexPath 
{ 
    NSDictionary *info = [json objectAtIndex:indexPath.row]; 
    ArticleViewController *articleViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ArticleViewController"]; 
    articleViewController.articleView = articleView; 
    [self.navigationController pushViewController: articleViewController animated:YES]; 
} 
+0

아마도'dvc.name = [self.names objectAtIndex : indexPath.row]; ' – Kevin

+0

:)라고 입력해야합니다. 페이지를 새로 고침해야합니다. 중간에 수정했습니다. – vikingosegundo

+0

필자는'DetailViewController'에 UILabel'myLabel'을 가지고 있습니다. 이것은 어떻게 작동 했는가입니다 (작동하지 않습니다) :'ArticleView * article = [self.storyboard instantiateViewControllerWithIdentifier : @ "ArticleView"]; [article.myLabel setText : @ "Johnny"]; ' –

0

에서

@interface ArticleViewController : UIViewController 
@property (retain) ArticleView *articleView; 
@end 

//Don't forget to synthesize name 

,이 방법 당신은 당신의 상세보기를 제시 당신은 당신이 여기에 아이폰을 이야기하고 가정 새로운 뷰 컨트롤러

0

을 만들 때 정보는 당신이 필요로하는, 내 상세보기에서 속성 이름을했을 :

@propery (nonatomic, retain) NSString * name; 

및 초기화 방법은 다음과 같다 :

- (id) initWithName: (NSString *) name 
{ 
    self = [super initWithNibName: @"<view-controller-nib-name>" bundle: nil]; 
    if (self) { 
     self.name = name; 
    } 
    return self; 
} 

후 설정된 이름 레이블링 tableView:didSelectRowAtIndexPath: 방법에 viewDidLoad

에서, 지수 경로에 의해 참조되는 이름을 가져, 생성 및 상세보기를 초기화하기 컨트롤러에 initWithName:을 사용하고 nav 스택을 누릅니다.

+0

- 여기서 NSString 속성을 유지하는 대신 복사해야하는 이유에 대해 설명합니다. http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain – vikingosegundo

관련 문제