【问题标题】:How to use correctly segue with a Tab Bar Controller?如何正确使用标签栏控制器?
【发布时间】:2013-04-11 18:36:09
【问题描述】:

我的应用程序使用不同的主细节,它包含在带有 tableView 的初始 ViewController 中,但是当用户单击导航控制器中的添加按钮时,我需要使用以下函数发送一个对象:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"AddDGV"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        [[segue destinationViewController] setDetailView:object];
    }
}

当目标是另一个 ViewController 时,代码可以正常工作,但我的应用程序设计为使用 TabBarController 而不是 ViewController。此 TabBar 有 2 个选项卡,每个选项卡都有不同的导航控制器,第一个是标识符“AddDGV”的目标。我创建了 .m/.h 文件并在情节提要中关联了类,并将实例变量“DetailView”设置为 ID。

@property (strong, nonatomic) id detailItem;

问题是编译器不知道这个变量,因为命令[segue destinationViewController] 包含视图控制器,其内容应该显示在segue 的末尾。由于segue的结尾是一个TabBarController,所以这个函数不起作用。

有人知道怎么解决吗???

【问题讨论】:

    标签: ios xcode uitabbarcontroller storyboard uistoryboardsegue


    【解决方案1】:

    的输出进行类型转换
    [segue destinationViewController]
    

    如下

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"AddDGV"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
            [(YourDestinationViewController *)[segue destinationViewController] setDetailView:object];
        }
    }
    

    【讨论】:

    • 编译器不直接接受这种类型转换,但是如果我之前从目标创建类型的变量,则可以正常工作。感谢您的帮助,我现在将尝试完成此应用程序。
    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多