简体   繁体   中英

How does a table view controller communicate with its detail view?

I have a table view, and if a certain row is tapped, the detail view will show up. But how does the detail view know which row was tapped ? So for instance, I have a MainController which displays names. If I tap "Johnny" The next screen should show a label with the string "Johnny". How would I do that ?

[EDIT - Added code]

- (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

-> Synthesizing properties

You could pass a object (ie the String Jonny) to the detail view controller as a property.

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

//Don't forget to synthesize name 

in the tableview controller

-(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];
}

表视图委托具有方法tableView:didSelectRowAtIndexPath: ,在此方法中,您将显示详细信息视图,以便知道从indexPath参数中选择了哪一行,并且可以在创建新视图控制器时传递所需的任何信息

Assuming you're talking iPhone here, I would have a property name on my detail view:

@propery (nonatomic, retain) NSString * name;

and an init method as follows:

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

and then set name label in viewDidLoad

In the tableView:didSelectRowAtIndexPath: method, get the name referenced by the index path, create and init a detail view controller using initWithName: , then push on the nav stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM