简体   繁体   English

如何在不显示数据的情况下将数据传递给ViewController?

[英]How to pass Data to a ViewController without presenting it?

i want to pass from a view controller 4 BOOL Values to my ChooseLevelVC: 我想从一个视图控制器4 BOOL值传递给我的ChooseLevelVC:

if (_moves >=4) {
    clvc.level1Completed3 = 1;
}
if (_moves == 3) {
    clvc.level1Completed2 = 1;
}
if (_moves == 2) {
    clvc.level1Completed1 = 1;
} else
    clvc.level1Completed = 1;

But i don't want to present ChooseLevelVC. 但我不想介绍ChooseLevelVC。 It should work in Background. 它应该在后台工作。 This Bool Values changes the Image of Buttons in ChooseLevelVC. 此布尔值更改ChooseLevelVC中按钮的图像。

My Code works fine if i present ChooseLevelVC with: 如果我通过以下方式提供ChooseLevelVC,则我的代码工作正常:

ChooseLevelViewController *clvc = [self.storyboard instantiateViewControllerWithIdentifier:@"chooseLevelViewController"];
if (_moves >=4) {
   clvc.level1Completed3 = 1;
}
if (_moves == 3) {
    clvc.level1Completed2 = 1;
}
if (_moves == 2) {
    clvc.level1Completed1 = 1;
 } else
    clvc.level1Completed = 1;
[self presentViewController:clvc animated:NO completion:^(){

 }];

if i don't present ChooseLevelVC, nothing happens. 如果我不显示ChooseLevelVC,则什么也不会发生。 Do i have to save the Values? 我必须保存值吗? When yes, how? 是的时候如何?

thanks! 谢谢!

You'll need to store your ChooseLevelViewController in a local property: 您需要将ChooseLevelViewController存储在本地属性中:

@property (nonatomic, strong) ChooseLevelViewController *chooseLevelViewController;

... so that you can instantiate it once in viewDidLoad : ...以便您可以在viewDidLoad实例化一次

- (void)viewDidLoad{
    [super viewDidLoad];

    // This is the only place you need to instantiate this view controller!
    self.chooseLevelViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"chooseLevelViewController"];
}

... and then configure it as needed: ...,然后根据需要进行配置:

self.chooseLevelViewController.level1Completed1 = 1;

... eventually presenting it whenever you like: ...最终在您愿意时展示:

[self presentViewController: self.chooseLevelViewController animated:NO];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不显示的情况下启动视图控制器? - How to initiate a viewcontroller without presenting it? 如何在UIViewControllers之间传递数据而不显示它 - How to pass data between UIViewControllers without presenting it 如何在没有Segue的情况下将数据传递到下一个viewController - How to pass data without Segue to next viewController 如何在多人游戏中以编程方式连接2个玩家而不显示默认的ViewController - How to programmatically connect 2 players in multiplayer match without presenting the default ViewController 如何在不创建对象的情况下将数据传递给viewController - How to pass data to viewController without creating its object 如何不使用segues将数据从一个viewController传递到另一个? - How to pass data from one viewController to another without using segues? 如何在不使用按钮的情况下快速将数据传递到另一个ViewController - How to pass data to another ViewController without using a button in swift 如何在ViewController和子视图ViewController之间传递数据 - How to pass data between a ViewController and a subview ViewController Swift:如何将数据从“didSelectRowAt”传递到另一个 VC 而不显示它或转场? - Swift: How do I pass data from "didSelectRowAt" to another VC without presenting it or a segue? 如何将数据传递给另一个Viewcontroller? - How to pass Data to Another Viewcontroller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM