objective-c - TableView Controller 如何与其详细 View 通信?

标签 objective-c ios cocoa-touch uitableview

我有一个 TableView ,如果点击某一行,就会显示详细 View 。但是详细 View 如何知道哪一行被点击了? 因此,例如,我有一个显示名称的 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

->综合属性

最佳答案

您可以将对象(即字符串 Jonny)作为属性传递给详细 View Controller 。

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

//Don't forget to synthesize name 

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

关于objective-c - TableView Controller 如何与其详细 View 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8943343/

相关文章:

ios - 苹果和 Stripe : Merchant id/apple pay certificate could not seen in xcode

iphone - 带有静态图像的 UINavigationBar

ios - 苹果 LLVM 6.1 错误 Xcode

objective-c - Objective C 语法查询

objective-c - 如何动画和旋转图像?

objective-c - 从 UITableView 中删除没有行的部分

iphone - 为什么 Brightcove 播放器未在 UIWebview 中加载?

ios - 未找到 PhoneGap 应用程序 : "ERROR: Start Page at ` www/index. html`”

ios - 致命异常 : NSRangeException

ios - 在 Storyboard中使用 Swift 嵌套类作为 UIViewController