使用segue传递数据而不显示屏幕



我试图使用segue实现数据传递,但不显示屏幕。在我的项目中有两个视图控制器,我将它们命名为FirstViewControllerSecondViewController

FirstViewController上,我使用Show自适应分段来使用performSegueWithIdentifier传递数据。将调用prepareForSegue方法,并识别我设置为显示SecondViewControlleridentifier

但是,问题是我不想显示SecondViewController。在前往SecondViewController之前,我的用户可能还想做其他事情。

我是iOS编程的新手,我只知道用segue传递数据。请与我分享是否有方法来传递除segue之外的数据。

编辑:进一步阐述我的问题。我使用的是TabBarController,两个View Controllers都可以在选项卡栏上访问。因此,当我在SecondViewController上,一些数据已经从FirstViewController"分割"过来时,我可以返回FirstViewController添加更多数据。在具有UIRefreshControlSecondViewController处,我需要更新的数据。

如果你在代码中执行segue,你可以用以下方式做你想做的事情

1) 从故事板实例化视图控制器

let storyboard: UIStoryboard = UIStoryboard(name: "SecondViewController", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("set_it_in_IB") as! SecondViewController

2) 设置您需要设置的属性,因为视图控制器已经实例化

vc.someProperty = "asd"

3) Segue!

viewController.presentViewController(viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?)

希望能有所帮助!

从用户的角度来看,他们在分割之前不知道数据是否正在更新,所以使用prepareFoSegue函数传递数据可能没问题。

否则,您可以尝试通过将通知设置为FirstViewControllerSecondViewController上的viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: nil, name: updateSecondVCNotification, object: nil)

以及每当需要使用更新数据时从CCD_ 23发布通知

NSNotificationCenter.defaultCenter().postNotificationName(updateSecondVCNotification, object: self)

然后在第二个视图中,控制器将addObserver的选择器设置为一个字符串,该字符串包含您希望它执行的任何函数的名称,如"reloadNewData"

func reloadNewData() { tableView.reloadData() }

这有帮助吗?不要忘记将updateSecondVCNotification设置为FirstViewController顶部的全局字符串常量。有关NSNotificationCenter的更多信息,请访问https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/

哇!通知(▽^)/

我不确定这是否适用,但您可以简单地将数据保存在FirstViewController的内存中,在用户仍在交互和提供反馈的时间内,一旦完成,就通过segue将所有数据发送到SecondViewController。

最新更新