【问题标题】:Send indexPath.row to another UIViewController将 indexPath.row 发送到另一个 UIViewController
【发布时间】:2012-09-01 18:44:38
【问题描述】:

所以我将 didSelectRowAtIndexPath 方法放入 UITableViewController

TableView 转到由 NavigationController 控制的 UIViewController

我想将 indexPath.row.row 发送到 NavigationController 调用的类,有没有办法将参数发送到另一个类

我的应用方案是:

TabBarController ---> NavigationController ---> TableViewController ---> UIViewController

【问题讨论】:

    标签: ios xcode uitableview navigationcontroller


    【解决方案1】:

    如果您使用情节提要:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        UITableViewCell *cell = sender;
        // Make sure your segue name in storyboard is the same as this line
        if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
        {
            // Get reference to the destination view controller
            YourViewController *vc = [segue destinationViewController];
            NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
            // Pass any objects to the view controller here, like...
            [vc setIndexPath:indexPath];
        }
    }
    

    否则

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (indexPath.row == 0) {
            YourViewController *vc = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
            [vc setIndexPath:indexPath];
        }
    
    }
    

    【讨论】:

    • 我正在使用情节提要:我必须在哪里调用 prepareForSegue 方法?我猜在 didSelectRowAtIndexPath
    • 而且.. 我在 initWithNibName 中调用的 UIViewController 不是 TableViewControll,因此它没有 setIndexPath 选择器..
    • 你不需要调用prepareForSegue,如果你将故事板中的tableViewCell中的segue拖到你的viewController,你必须将它命名为@"YOUR_SEGUE_NAME_HERE",然后当用户点击单元格时,prepareForSegue方法被调用。如果你想传递数据,你需要在 UIViewController 中有一些 @property,例如 NSIndexPath 或 NSInteger 行,并且当你传递时只需设置属性 [vc setRow:indexPath.row];
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多