【问题标题】:Can you return data via dismissModalViewControllerAnimated: or do you need a segue for the return journey?你可以通过dismissModalViewControllerAnimated返回数据:还是你需要一个segue来回程?
【发布时间】:2012-06-06 21:45:35
【问题描述】:

我有一个ViewController,它有一个UIButton,它执行以下操作:

- (IBAction)buttonClicked:(id)sender 
{
    NSLog(@"Button clicked, lets move to next controller to do stuff");
    [self performSegueWithIdentifier:@"toNextController" sender:nil];
}

这只是转移到我的下一个ViewController,到目前为止没有什么了不起的。

在第二个ViewController中,我会做一些我的应用逻辑,然后返回。

- (IBAction)backButtonPressed:(id)sender 
{
    NSLog(@"Back button clicked, lets just drop out of here...");
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)saveButtonPressed:(id)sender 
{
    NSLog(@"save button clicked, lets send some data back");

    [self performSegueWithIdentifier:@"backToMain" sender:nil];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{    
    if ([segue.identifier isEqualToString:@"backToMain"])
    {
        NSLog(@"Preparing segue for backToMain");

        // Obtain handles on the current and destination controllers
        MainController * startingViewController;
        SecondController * destinationController;

        startingViewController = (MainController * ) segue.sourceViewController;
        destinationController = (SecondController * ) segue.destinationViewController;

        // set data on the main controller
        startingViewController.myString = @"SomeDummyString";
    }
}

到目前为止,我尝试做的是创建第二个链接回主控制器的转场,在执行转场之前,抓住它的句柄并设置数据。我不确定这是否是返回的最佳方式。

问题:

是否有可能,在执行[self dismissModalViewControllerAnimated:YES]; 时返回数据,或者您是否需要为回程实现一个segue?

【问题讨论】:

  • 我不会对第一个控制器执行另一个 segue。据我了解,这会创建一个全新的 Controller 类实例,实际上,您只是在添加越来越多的控制器,因为它们彼此来回切换。我会使用委托/协议方法。看到这个:stackoverflow.com/questions/10841653/…

标签: objective-c ios ios5 storyboard segue


【解决方案1】:

看看这个:10841897

它描述了使用委托和协议来回发送信息。它可以根据您的需要稍作更改,只需创建一个 savedWithData: 方法,将字典或您想要的任何数据发送回第一个视图控制器,而不是链接中描述的通用 done

【讨论】:

    【解决方案2】:

    你可以这样做

    [destinationController setSomeData:@"Sending Something Back"];
    

    这将在你的destinationController 加载之前设置一个@property。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      相关资源
      最近更新 更多