本文介绍了使用Swift中其他视图中的视图输入文本(Xcode 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swift和Xcode几乎是全新的,所以请原谅我在这个领域缺乏知识。
目前我有一个带有标签的视图控制器。我想在此之前添加一个视图,使用文本字段,用户在其中输入名称,然后应用程序转到第二个视图,其中标签中用户输入的名称放在标签上。

I am almost completely new to Swift and Xcode so excuse my lack of knowledge in this field.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.

谢谢!

编辑:我对此知之甚少。使用两个视图时是否需要导航控制器?我需要两个ViewController.swift文件吗?

I really know little about this. Do I need a navigation controller when using two views? Do I need two ViewController.swift files?

推荐答案

以下是通过12个简单步骤完成的方法:

Here's how you do it in 12 easy steps:

1)是的。我建议使用导航控制器。使用单一视图应用程序启动一个新项目。

1) Yes. I recommend using a Navigation Controller. Start a new project with a Single View Application.

2)选择 ViewController 在故事板中,从上面的菜单栏中选择编辑器 - >嵌入 - >导航控制器

2) Select the ViewController in the Storyboard and from the Menu bar above select Editor->Embed In->Navigation Controller.

3)拖出 UIButton UITextField 并将它们放入你的第一个 ViewController

3) Drag out a UIButton and a UITextField and put them in your first ViewController.

4)你需要一个 IBOutlet 到你的文本字段,这样你就可以从中读取文字。 通过单击 Xcode 右上角的 Tuxedo 图标显示助理编辑器。单击Storyboard中的 ViewController 。右侧编辑器窗格将显示 ViewController 的代码。按住 Control 键并从文本字段拖动到右窗格中的代码。当你在类ViewController:UIViewController {下面的空间时,放开鼠标按钮。在弹出窗口中,将 Connection 设置为 Outlet ,将名称设置为 textField ,然后按连接。这会将行 @IBOutlet weak var textField:UITextField!添加到 ViewController

4) You'll want an IBOutlet to your text field so that you can read the text from it. Show the Assistant editor by clicking on the Tuxedo icon in the upper right corner of Xcode. Click on the ViewController in the Storyboard. The right editor pane will show the code for ViewController. Hold down the Control key and drag from the text field to the code in the right pane. Let go on the mouse button when you are in the space just below class ViewController : UIViewController {. In the popup, set Connection to Outlet, set the name to textField and press Connect. This will add the line @IBOutlet weak var textField: UITextField! to your ViewController.

5)现在是时候添加第二个视图控制器了。从菜单中选择文件 - >新建>文件... 。在对话框中,确保左侧选择 iOS 下的 Source ,然后选择 Cocoa Touch Class 并按 Next 。将类命名为 SecondViewController ,并使其成为 UIViewController 的子类。按下一步然后创建

5) Now it is time to add a second view controller. From the menu select File->New->File.... In the dialog, make sure Source under iOS is selected on the left, and choose Cocoa Touch Class and press Next. Name the class SecondViewController and make it a subclass of UIViewController. Press Next and then Create.

6)在故事板中,拖出 UIViewController 并将其放在第一个 ViewController 的右侧。选择新的 ViewController 并通过单击 Tuxedo Identity Inspector / em>图标。 (提示:如果你将图标悬停,它会告诉你他们做了什么)。将类更改为 SecondViewController

6) In the Storyboard, drag out a UIViewController and drop it to the right of the first 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. (Hint: if you hover of the icons it will tell you what they do). Change the class to SecondViewController.

7)在故事板中,按住Ctrl键(按住控件并拖动)从第一个 ViewController 中的按钮到新的 SecondViewController 。在弹出窗口中选择显示作为动作搜索

7) In the Storyboard, control-drag (hold down control and drag) from the button in the first ViewController to the new SecondViewController. In the pop-up select Show as the Action Segue.

8)拖动标签 out并将其放入 SecondViewController 。像在步骤4中一样添加 IBOutlet ,并将其命名为 mylabel

8) Drag a Label out and put it into the SecondViewController. Add an IBOutlet to it like you did in step 4 and call it mylabel.

9)在 SecondViewController 的代码中添加一个新的实例变量,如下所示: var firstVCtext =

9) In the code for SecondViewController add a new instance variable like so: var firstVCtext = ""

10)在第一个 ViewController 中添加此方法:

10) In the first ViewController add this method:

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

11)在 SecondViewController 中,添加这行代码到 viewDidLoad 方法: mylabel.text = firstVCtext

11) In SecondViewController, add this line of code to the viewDidLoad method: mylabel.text = firstVCtext

12)运行程序!

这篇关于使用Swift中其他视图中的视图输入文本(Xcode 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:10