简体   繁体   English

在 Swift (Xcode 6) 的其他视图中使用视图中输入的文本

[英]Use entered text from view in other view in Swift (Xcode 6)

I am almost completely new to Swift and Xcode so excuse my lack of knowledge in this field.我对 Swift 和 Xcode 几乎完全陌生,所以请原谅我缺乏这方面的知识。 At the moment I have a view controller with a label in it.目前我有一个带有标签的视图控制器。 I want to add a view before that, with a text field in which a user enters a name, the app then goes to the second view with the label in which the name entered by the user is placed on the label.我想在此之前添加一个视图,带有用户输入名称的文本字段,然后应用程序转到带有标签的第二个视图,其中用户输入的名称放置在标签上。

I know this is very basic, but I cannot find any good solutions since I'm not quite sure what to search for.我知道这是非常基本的,但我找不到任何好的解决方案,因为我不太确定要搜索什么。 If anyone has a piece of code or a link to a good source, I'd love to see it.如果有人有一段代码或一个好的来源的链接,我很乐意看到它。

Thanks!谢谢!

Edit: I really know little about this.编辑:我真的对此知之甚少。 Do I need a navigation controller when using two views?使用两个视图时是否需要导航控制器? Do I need two ViewController.swift files?我需要两个 ViewController.swift 文件吗?

Here's how you do it in 12 easy steps:以下是您通过 12 个简单步骤完成的方法:

1) Yes. 1) 是的。 I recommend using a Navigation Controller .我建议使用Navigation Controller Start a new project with a Single View Application .使用Single View Application开始一个新项目。

2) Select the ViewController in the Storyboard and from the Menu bar above select Editor->Embed In->Navigation Controller . 2) 在 Storyboard 中选择ViewController并从上方的菜单栏中选择Editor->Embed In->Navigation Controller

3) Drag out a UIButton and a UITextField and put them in your first ViewController . 3) 拖出一个UIButton和一个UITextField并将它们放在你的第一个ViewController

4) You'll want an IBOutlet to your text field so that you can read the text from it. 4) 您需要一个IBOutlet到您的文本字段,以便您可以从中读取文本。 Show the Assistant editor by clicking on the Tuxedo icon in the upper right corner of Xcode .单击Xcode右上角的Tuxedo图标,显示 Assistant 编辑器 Click on the ViewController in the Storyboard.单击 Storyboard 中的ViewController The right editor pane will show the code for ViewController .右侧的编辑器窗格将显示ViewController的代码。 Hold down the Control key and drag from the text field to the code in the right pane.按住Control键并从文本字段拖动到右侧窗格中的代码。 Let go on the mouse button when you are in the space just below class ViewController : UIViewController { .当您位于class ViewController : UIViewController {下方的空间时,松开鼠标按钮class ViewController : UIViewController { In the popup, set Connection to Outlet , set the name to textField and press Connect .在弹出窗口中,将Connection设置为Outlet ,将名称设置为textField并按Connect This will add the line @IBOutlet weak var textField: UITextField!这将添加行@IBOutlet weak var textField: UITextField! to your ViewController .到您的ViewController

5) Now it is time to add a second view controller. 5) 现在是添加第二个视图控制器的时候了。 From the menu select File->New->File... .从菜单中选择File->New->File... In the dialog, make sure Source under iOS is selected on the left, and choose Cocoa Touch Class and press Next .在对话框中,确保在左侧选择iOS下的Source ,然后选择Cocoa Touch Class并按Next Name the class SecondViewController and make it a subclass of UIViewController .将类命名为SecondViewController并使其成为UIViewController的子类。 Press Next and then Create .下一步,然后创建

6) In the Storyboard, drag out a UIViewController and drop it to the right of the first ViewController . 6) 在 Storyboard 中,拖出一个UIViewController并将其放到第一个ViewController的右侧。 Select the new ViewController and open the Identity Inspector by clicking on the 3rd icon from the left in the row of icons below the Tuxedo icon.选择新的ViewController并通过单击Tuxedo图标下方图标行中从左侧开始的第三个图标打开Identity Inspector (Hint: if you hover of the icons it will tell you what they do). (提示:如果您将鼠标悬停在图标上,它会告诉您它们的作用)。 Change the class to SecondViewController .将类更改为SecondViewController

7) In the Storyboard, control-drag (hold down control and drag) from the button in the first ViewController to the new SecondViewController . 7) 在 Storyboard 中,从第一个ViewController的按钮控制拖动(按住控制并拖动)到新的SecondViewController In the pop-up select Show as the Action Segue .在弹出窗口中选择Show as the Action Segue

8) Drag a Label out and put it into the SecondViewController . 8) 拖出一个Label并将其放入SecondViewController Add an IBOutlet to it like you did in step 4 and call it mylabel .像您在步骤 4 中所做的那样向其中添加一个IBOutlet并将其命名为mylabel

9) In the code for SecondViewController add a new instance variable like so: var firstVCtext = "" 9) 在SecondViewController的代码中添加一个新的实例变量,如下所示: var firstVCtext = ""

10) In the first ViewController add this method: 10) 在第一个ViewController添加这个方法:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    let secondVC = segue.destinationViewController as SecondViewController
    secondVC.firstVCtext = textField.text
}

11) In SecondViewController , add this line of code to the viewDidLoad method: mylabel.text = firstVCtext 11) 在SecondViewController ,将这行代码添加到viewDidLoad方法中: mylabel.text = firstVCtext

12) Run the program! 12) 运行程序!

Another good tutorial here with code and images:另一个很好的教程,这里有代码和图像:

http://www.jamesrambles.com/ios-swift-passing-data-between-viewcontrollers/ http://www.jamesrambles.com/ios-swift-passing-data-between-viewcontrollers/

To move data from one view to another, use of a segue (pronounced seg-way) is the easiest.要将数据从一个视图移动到另一个视图,使用 segue(发音为 seg-way)是最简单的。 One example is here:一个例子在这里:

http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/ http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/

If you Google for Swift and Segue you will likely find more examples.如果您使用 Google 搜索 Swift 和 Segue,您可能会找到更多示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM