简体   繁体   English

将数据从一个视图控制器传递到下一个视图控制器,而无需快速进行选择

[英]Pass data from one view controller to the next WITHOUT a segue in swift

So I have an app that where you choose a button in the first screen it will bring you to another screen depending on which one you chose on the first one. 因此,我有一个应用程序,您可以在第一个屏幕上选择一个按钮,根据您在第一个屏幕上选择的按钮,将您带到另一个屏幕。 Because I have these possible different layouts I can't just use a segue from one button to the next view controller because there is a middle screen involved. 因为我有这些可能的不同布局,所以不能只使用从一个按钮到下一个视图控制器的顺序,因为其中涉及中间屏幕。 SO it is like this: 所以是这样的:

1 ---> 2 ---> 3a || 1 ---> 2 ---> 3a || 3b || 3b || 3c || 3c || 3d etc... 3D等...

So your choice on the first screen dictates what your third screen will look like. 因此,您在第一个屏幕上的选择决定了第三个屏幕的外观。

So instead I was thinking to have a value associated with each button in the first view, have it sent to the second view via a segue, and then sent again to the third view and tell it to use the proper segue to the proper third view Controller based on the value associated with the first view. 因此,我想让它在第一个视图中与每个按钮关联一个值,然后将它通过segue发送到第二个视图,然后再次发送到第三个视图,并告诉它对正确的第三个视图使用正确的segue控制器基于与第一个视图关联的值。 But I can't do that because I can't set the second screen to segue to all the possible layouts for the third screen...At least i think I can't. 但是我不能这样做,因为我无法将第二个屏幕设置为适合第三个屏幕的所有可能布局...至少我认为我不能。

Bottom line question: is there anyway to go to another view controller after clicking a button without a segue while also sending data to that next view controller, again, without a segue to store it in? 底线问题:单击按钮后,是否仍然可以转到另一个视图控制器,而又没有数据存储到另一个视图控制器,而又再次将数据发送到该视图控制器?

Please help mehhh 请帮忙

Follow these steps... 跟着这些步骤...

1: Create a Separate file called Manager.swift and place this code in it... 1:创建一个名为Manager.swift的单独文件,并将此代码放入其中...

//manager.swift

    import Foundation

    struct Manager {

    static var messageText = String()


    }

2: Clean your project by pressing Shift+Command+K. 2:通过按Shift + Command + K清除项目。

3: In the first view controller set the messageText to the data you want to pass... 3:在第一个视图控制器中,将messageText设置为要传递的数据...

Manager.messageText = self.dataToPass

4: In the second view controller retrieve the data and set the content to the message text... 4:在第二个视图控制器中检索数据并将内容设置为消息文本...

self.dataToReceive = Manager.messageText

5: Your Finished!! 5:完成!!

So instead I was thinking to have a value associated with each button in the first view, have it sent to the second view via a segue, and then sent again to the third view and tell it to use the proper segue to the proper third view Controller based on the value associated with the first view. 因此,我想让它在第一个视图中与每个按钮关联一个值,然后将它通过segue发送到第二个视图,然后再次发送到第三个视图,并告诉它对正确的第三个视图使用正确的segue控制器基于与第一个视图关联的值。 But I can't do that because I can't set the second screen to segue to all the possible layouts for the third screen...At least i think I can't. 但是我不能这样做,因为我无法将第二个屏幕设置为适合第三个屏幕的所有可能布局...至少我认为我不能。

You can do this with segues; 可以使用segues执行此操作; you just have to wire them up properly. 您只需要正确地连接它们即可。 Instead of hooking the segues up to buttons, you can create them by control -dragging from the ViewController icon at the top to the next ViewController. 您可以通过按Control键并将其从顶部的ViewController图标拖动到下一个ViewController,而不是将Segues固定在按钮上。

从视图控制器图标中选择

Once you have created such a segue, click on the resulting segue arrow and give it an identifier in the Attributes Inspector (something like "segueToVC3a"). 创建了这样的序列后,请单击生成的序列箭头,并在“ 属性”检查器中为其指定一个标识符(类似“ segueToVC3a”)。

输入segue标识符

So, you would do this multiple times for each segue from VC2 to VC3a, VC2 to VC3b, etc. 因此,对于从VC2到VC3a,从VC2到VC3b等的每个序列,您将多次执行此操作。

Assuming in your first viewController you decide the value of whereToGo and you pass that to your second viewController with a segue. 假设在你第一次的viewController你决定的价值whereToGo ,你传递给你的一个SEGUE第二的viewController。 Then in your second viewController, you could then do something like this: 然后在第二个viewController中,您可以执行以下操作:

var identifier: String = ""
switch whereToGo {
    case "VC3a": identifier = "segueToVC3a"
    case "VC3b": identifier = "segueToVC3b"
    default: break
}

if identifier != "" {
    performSegueWithIdentifer(identifier, sender: self)
}

暂无
暂无

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

相关问题 我正在尝试通过“准备转场”将数据从一个视图控制器传递到另一个视图控制器,但它崩溃了 - I am trying to pass data from one View Controller to another in Swift via 'prepare for segue' but it crashes 在不使用Segue的情况下将数据从一个视图控制器传递到另一个 - Passing data from one view controller to another without using Segue 停止Segue并传递到下一个视图,警报中显示的文本字段的内容-Swift - Stop Segue and pass to next view the content of textfield shown in alert - Swift 如何快速将NSDictionary传递给下一视图控制器 - How to pass NSDictionary to next view controller in swift 演示过程中尝试在&lt;**&gt;上演示&lt;**&gt;。 在使用swift从一个视图控制器到另一个视图控制器执行segue的同时 - Attempting to present <**> on <**> while a presentation is in progress. While performing a segue from one view controller to another using swift 如何将UI ImageView从一个视图控制器传递到另一个? 迅捷3 - how to pass a UI ImageView from one view controller to another? swift 3 Swift:从camera / UIImagePickerController直接查看视图控制器 - Swift: Segue directly to a view controller from camera/UIImagePickerController 如何在IOS中将数据从一个视图传递到另一个视图控制器? - How to pass data from one view to another view controller in IOS? 如何使用segue将图像从视图控制器传递到选项卡栏? - how to pass image from view controller to tab bar with segue? 如何在视图控制器之间传递数据而不在 ios 中使用 segue? - How to pas data between view controller without using segue in ios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM