【问题标题】:Perform Segue programmatically and pass parameters to the destination view以编程方式执行 Segue 并将参数传递给目标视图
【发布时间】:2012-03-04 03:54:38
【问题描述】:

在我的应用程序中,我有一个以编程方式执行 segue 的按钮:

- (void)myButtonMethod
{
    //execute segue programmatically
    [self performSegueWithIdentifier: @"MySegue" sender: self];
}

我想知道是否有办法引用目标视图并传递一些参数。

我知道在prepareForSegue 方法中,我可以使用:myDestinationViewController *vc = [segue destinationViewController]; 来引用它,但我不知道如何以编程方式执行segue。

你有什么想法吗?

谢谢你,亚萨


更新:

对不起这个问题!!!我只是发现,即使以编程方式调用 segue,无论如何都会调用 prepareForSegue 方法,因此可以以相同的方式传递参数。

【问题讨论】:

  • 将更新添加为答案并接受它,以便人们知道此问题已得到回答:)
  • 添加答案,评论通知。
  • 您应该将答案below 标记为正确,以便人们知道它已被回答。

标签: ios view parameters storyboard segue


【解决方案1】:

答案很简单,segue 的触发方式无关紧要。

prepareForSegue:sender: 方法在任何情况下都会被调用,这是您传递参数的地方。

【讨论】:

  • 这仍然困扰着我 - 这意味着我必须在一个地方触发 segue,在那里我有我想要作为参数发送的数据,与其他任何东西都很好地隔离,但是要完全添加该数据不同的地方 - prepareForSegue,我们对需要传递的数据一无所知。意思是,我必须在两个不同功能(一个触发 segue,另一个 - 准备)共享的上下文中创建一个包含相关数据的状态。当我只需要用一些数据打开一个新视图时,我不想弄脏状态......
  • 使用发件人传递配置目标 VC 所需的上下文
  • 我认为在发件人中传递有效负载是一种反模式。它应该只包含有关谁触发了 segue 的信息,而不是触发 segue 的东西打算进一步传递的数据。
  • 来自 sender 的官方文档:“此对象在实际转场期间可用于提供信息。”
  • 是的,不要传递实际的有效负载,传递上下文,比如哪个单元格/按钮/索引/等触发了 segue。
【解决方案2】:

老问题,但这里是关于如何做你所问的代码。在这种情况下,我将数据从表格视图中的选定单元格传递到另一个视图控制器。

在trget视图的.h文件中:

@property(weak, nonatomic)  NSObject* dataModel;

在.m文件中:

@synthesize dataModel;

dataModel 可以是stringint,或者在本例中,它是一个包含许多项目的模型

- (void)someMethod {
     [self performSegueWithIdentifier:@"loginMainSegue" sender:self];
 }

或者...

- (void)someMethod {
    UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
    [self.navigationController pushViewController: myController animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"storyDetailsSegway"]) {
        UITableViewCell *cell = (UITableViewCell *) sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSDictionary *storiesDict =[topStories objectAtIndex:[indexPath row]];
        StoryModel *storyModel = [[StoryModel alloc] init];
        storyModel = storiesDict;
        StoryDetails *controller = (StoryDetails *)segue.destinationViewController;
        controller.dataModel= storyModel;
    }
}

【讨论】:

    【解决方案3】:

    我了解在一个地方执行转场并保持状态以发送参数以准备转场的问题。

    我想出了一个办法。我使用类别向 ViewControllers 添加了一个名为 userInfoDict 的属性。而且我也用标识符覆盖了执行segue,这样如果发件人是自己的(意味着控制器本身)。它将这个 userInfoDict 传递给下一个 ViewController。

    在这里,除了传递整个 UserInfoDict 之外,您还可以传递特定的参数,作为发件人并相应地覆盖。

    您需要记住的一件事。不要忘记在你的 performSegue 方法中调用 super 方法。

    【讨论】:

      【解决方案4】:

      如果您使用新的 swift 版本。

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              if segue.identifier == "ChannelMoreSegue" {
      
              }
      }
      

      【讨论】:

        【解决方案5】:

        斯威夫特 4:

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "ExampleSegueIdentifier" {
                if let destinationVC = segue.destination as? ExampleSegueVC {
                    destinationVC.exampleString = "Example"
                }
            }
        }
        

        斯威夫特 3:

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                if segue.identifier == "ExampleSegueIdentifier" {
                    if let destinationVC = segue.destinationViewController as? ExampleSegueVC {
                        destinationVC.exampleString = "Example"
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-03
          • 1970-01-01
          • 2017-12-28
          • 1970-01-01
          • 1970-01-01
          • 2023-03-14
          • 1970-01-01
          相关资源
          最近更新 更多