【问题标题】:iOS/Xcode - Multiple segue identifiers resetting data parsingiOS/Xcode - 多个 segue 标识符重置数据解析
【发布时间】:2013-05-15 14:25:21
【问题描述】:

所以我得到了一个带有 4 个单独按钮的 ViewController。单击 button1 时,TableViewController1 会弹出带有项目列表的 ViewController。选择项目时,TableViewController1 会下拉,并且 button1 现在具有在表格中选择的文本。这一切都很好。但是,当我使用 TableViewController2 对 button2 执行完全相同的操作时,来自 button1 的数据将被重置。

我使用带有标识符的segues,一些代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showEducation"]) {
        NSIndexPath *indexPath = [self.tableViewEducation indexPathForSelectedRow];
        ViewController *destViewController = segue.destinationViewController;
        destViewController.educationText = [tableViewArray objectAtIndex:indexPath.row];
    }
}

所以目前我为每个按钮获得了多个 segue 标识符,为 tableviews 获得了多个 .h 和 .m 文件。我是否使用了完全错误的技术来使其正常工作?我希望我足够清楚,否则我可以上传图片。

编辑:我刚刚注意到,我的 ViewController 上还有一个滑块。单击按钮并在 TableView 中选择一行时,滑块将重置为原始位置。和上面一样的问题。

【问题讨论】:

  • 您的两个独立的 TableViewController 对象是否使用相同的对象/类作为其数据源?
  • 你到底是什么意思?
  • 一个类或对象有你的“cellForRowAtIndexPath”方法,还是每个 TableViewController 对象都有自己的?
  • 每个 TableViewController 都有自己的。这是错的吗?
  • 如果不了解您在控制器之间来回切换的方式,很难判断您在做什么。你什么时候看到按钮 1 的数据被重置了——你是回到第一个控制器看到的,还是总是能看到这 4 个按钮?

标签: ios tableview segue


【解决方案1】:

我认为您每次从 tableViewController 推送时都会推送到视图控制器的新实例。

假设您单击 ViewController0 上的一个按钮,这将创建 tableViewController1 的一个实例。当您单击一行时,您只是在使用 performSegue 创建 ViewController0 的新实例,它有自己的 ViewDidLoad - 重置按钮。

(您是说视图“下拉”,所以它是模态的?) 不要从 tableViewController 使用 performSegue 回到 viewController,尝试使用 [self dismissModalViewController: withCompletion:](或类似的东西,不记得了),然后你的 tableViewController 应该删除自己并显示原来的 ViewController。

现在,您无法更改按钮的名称,但可以通过从 tableView 访问 sender 来完成,这将为您提供原始视图控制器,而不是新实例。

获取发送者的一种方法是使用 ViewController0 中的[performSegue...,在它自己的prepareForSegue 中,您可以执行类似的操作

//In the first ViewController, not in the TableViewControllers
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender{
    if(sender == button1)
    {
        UITableViewController1 *dest = segue.destinationViewController;
        [dest setSender: self];
    }
}

在 TableViewController1 中,您将创建一个变量 ViewController *home; 和一个方法 -(void)setSender:(ViewController*)sender;,以便在您的 didSelectRowAtIndexPath 中,您现在可以说 [[(ViewController0*)home button1]setTitle:@..];,然后是 [dismissModalViewController..]

还有其他方法可以做到这一点,这取决于您如何从 viewController 推送到 tableViewController。而且我确信有比这更简单的方法来访问发件人,但是如果您已经在发送其他数据,它会起作用并且很有用。

【讨论】:

  • 哇,多么好的解释。如果我读到这似乎很合乎逻辑,我会尝试代码,但我还不知道如何实现它。
  • 顺便说一句,我没有使用 performSegue,我通过情节提要在视图之间制作动画。
  • @Torylon 那么,在情节提要中,您是否从 viewController 到 tableViewController 以及从 tableViewController 到 viewController 的另一行进行了 segue(视图之间的这些行之一)?因为那是错误的。删除从 tableViewController 开始的 segue(line),并在 didSelectRowAtIndexPath 内部尝试 [self dismissModalViewController...] 或 [self.navigationController popViewControllerAnimated:YES];看看它是否正确响应
  • 是的,我刚刚删除了从 tableView 到 ViewController 的 segue。我没有使用 didSelectRowAtIndexPath 那么我在哪里实现呢?
  • - (void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // [self.navigationController popViewControllerAnimated:YES]; //[selfdismissModalViewController...] //[selfdismissModalViewControllerAnimated:YES]; [[self presentingViewController]dismissViewControllerAnimated:YES 完成:nil]; } 尝试了各种方法,它似乎并没有消除任何东西。
猜你喜欢
  • 2018-08-24
  • 2013-12-08
  • 2017-02-15
  • 1970-01-01
  • 2020-07-16
  • 2012-06-20
  • 1970-01-01
  • 2017-06-01
  • 2014-08-24
相关资源
最近更新 更多