【问题标题】:How To Use Storyboard?如何使用故事板?
【发布时间】:2011-12-14 15:31:44
【问题描述】:

我正在尝试使用故事板 segue 而不是 didSelectRowAtIndex。

这是我有的,但是这个方法不能识别 indexPath.row。有没有其他方法可以用来做同样的事情?

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    SpecificExerciseTableViewController *upcomingViewController = segue.destinationViewController;
    if ([segue.identifier isEqualToString:@"Web"]) 
    {
        upcomingViewController.exerciseArray = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
        upcomingViewController.muscleName = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"muscleName"];
    }
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios5 storyboard


    【解决方案1】:

    除非您将 indexPath 作为 ivar,否则范围内不存在此类变量。

    如果您想在 UITableView 中获取所选行的 indexPath,请使用:

    - (NSIndexPath *)indexPathForSelectedRow
    

    以上调整后的代码:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    
        SpecificExerciseTableViewController *upcomingViewController = segue.destinationViewController;
        if ([segue.identifier isEqualToString:@"Web"]) 
        {
            upcomingViewController.exerciseArray = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"exercises"];
            upcomingViewController.muscleName = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:@"muscleName"];
        }
    }
    

    【讨论】:

    • 我尝试做NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];,但是当我 NSLog 时它给了我一个空值。
    • 如果没有选定的行,它应该返回 nil,如果它仍然返回 nil。那么可能 tableView 属性为零。没有更多信息很难说。
    • 非常感谢 - 获得 NSIndexPath 是我所追求的。全部排序。
    【解决方案2】:

    这个技巧很好用,这是我如何使用它来将解析的元素拉入 web 视图的示例

    .h 文件

    @property (strong, nonatomic) NSString *url;
    @property (strong, nonatomic) UIWebView *webView;
    

    .m 文件

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        if ([segue.identifier isEqualToString:@"WebViewSegue"]) {
    
            WebViewController *webViewController = segue.destinationViewController;
            webViewController.url = (self.parseResults)[indexPath.row][@"link"];
            webViewController.title = (self.parseResults)[indexPath.row][@"title"];
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-22
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2016-11-09
      相关资源
      最近更新 更多