xcode 在 prepareForSegue 中设置 UITextField 文本属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18137073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:41:00  来源:igfitidea点击:

Set UITextField text property in prepareForSegue

iosobjective-cxcodeuitextfieldsegue

提问by Cody Winton

I have a textField in my ViewController1. I have a push segue to my ViewController2 which also has a textField. I need to set the textField.text in my ViewController2 to be equal to the text in the textField in my ViewController1. Here's the code I have:

我的 ViewController1 中有一个 textField。我有一个到我的 ViewController2 的 push segue,它也有一个 textField。我需要将 ViewController2 中的 textField.text 设置为等于我的 ViewController1 中 textField 中的文本。这是我的代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqual:@"ViewController2"])
    {
        ViewController2 *VCT = [segue destinationViewController];
        VCT.title = @"View Controller #2";
        VCT.textField.text = self.textField.text;
    }
}

The title setter works just fine. Why is the textField not working?

标题设置器工作得很好。为什么 textField 不起作用?

回答by Lucas Eduardo

You cannot set the text of the UITextField(and any other components like UILabeland UITextView) in the prepare for segue, because all view components of the recently allocated controller are not initialized yet (at this time, they are all nil), they only will be when the viewController is presented (and it's view loaded).

你不能在准备转场中设置UITextField(以及任何其他组件,如UILabelUITextView)的文本,因为最近分配的控制器的所有视图组件都还没有初始化(此时,它们都是nil),它们只会在viewController 被呈现(并且它的视图已加载)。

You have to create a NSStringproperty in the presented viewController, and then, somewhere inside your another view controller, like viewDidLoad, set the text of the textField as this property value.

您必须NSString在呈现的 viewController 中创建一个属性,然后在另一个视图控制器中的某处,例如viewDidLoad,将 textField 的文本设置为该属性值。

Like this:

像这样:

In the viewController that will present the another viewController:

在将呈现另一个 viewController 的 viewController 中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ViewController2"])
    {
        ViewController2 *vc2 = [segue destinationViewController];
        vc2.myString = self.textField.text;
    }
}

And inside the presented viewController:

在呈现的 viewController 中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    (...)
    self.textField.text = self.myString;
}

回答by Michael Dautermann

Check and see if "VCT.textField" is not nil at that point. I suspect that you'll find it is nil.

检查并查看此时“ VCT.textField”是否不为零。我怀疑你会发现它是零。

You either need to expose it via "@property (strong) IBOutlet UITextField * textField" or you need to store the string somewhere else (e.g. a "NSString" property?) which then loads the text field when the view is fully loaded.

您要么需要通过“ @property (strong) IBOutlet UITextField * textField”公开它,要么需要将字符串存储在其他地方(例如“ NSString”属性?),然后在视图完全加载时加载文本字段。