ios - 之间传递数据

标签 ios objective-c didselectrowatindexpath

我有两个由 segue 连接的 ViewController,第一个是 Sub View,第二个是 Sub Tabela 我想将该表中选定行的值从我的第一个 View Controller 传递到我的第二个 View Controller ,这样我就可以定义他的标题。 这是代码。

SubView.m(我的第一个 ViewController)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // _vinhoArray is the array that I populate the TableView
    NSString *selectedRow = [_vinhoArray objectAtIndex:indexPath.row];
    // Sub Tabela is the name of Second View
    SubTabela *subTabela = [[SubTabela alloc] init];
    // tituloTabela is a NSString in my Second View
    subTabela.tituloTabela = linhaSelecionada;

    // Here i get the Right value
    NSLog(@"value %@", subTabela.tituloTabela);

}

SubTabela.h(我的第二个 ViewController)

@property (nonatomic, strong) NSString *tituloTabela;

SubTabela.m

@synthesize tituloTabela;

- (void)viewDidLoad
{

    [super viewDidLoad];

    self.title = tituloTabela;

    // Here i get the a Null value
    NSLog(@"value %@", tituloTabela);

}

请帮忙!

最佳答案

我认为您没有正确传递数据,看起来您正在 didSelectRowAtIndexPath 中创建一个对象并将数据传递到其中,然后丢弃该对象。你需要做的是调用这个:

[self performSegueWithIdentifier:<#(NSString *)#> sender:self];

改变屏幕然后像这样传递数据是prepareForSegue回调

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SubTabela *vc = (SubTabela *)segue.destinationViewController;
    vc.tituloTabela = linhaSelecionada;
}

关于ios - 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21237894/

相关文章:

ios - 解析 PFQueryTableViewController numberOfRowsInSection 错误

ios - 在 isKindOfClass : 中未检测到从 UIButton 继承的自定义类

ios - MPMediaItemArtwork 和透明度

ios - 这个 Int32 初始化器能返回 nil 吗?

objective-c - NSDate dateWithTimeIntervalSince1970 在控制台显示错误的时间戳

ios - 使用 iOS 7 的快照 API 模糊屏幕

ios - -didSelectRowAtIndexPath : not being called

ios - 使用 FRC Swift 3 更改 CoreData 后,TableView 不会更新(reloadData)

ios - 试图在 UITableView 内向 UILabel 添加点击手势 - 未调用点击手势

swift - 如何打开 View Controller 的同一实例?