获取 texfield Xcode 的数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13456506/
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
Get the number value of a texfield Xcode
提问by wmichaelsen
I want to have a textfield where the user can input a number and then set a label to that value. Is there any "texfield.value" or something that can fetch the value in numbers? This is the only piece i heve done. should i put it here?:
我想要一个文本字段,用户可以在其中输入一个数字,然后将标签设置为该值。是否有任何“texfield.value”或可以获取数字值的东西?这是我唯一完成的作品。我应该把它放在这里吗?:
- (IBAction)set:(id)sender;
{
{
}
}
Thanks in advance
提前致谢
回答by Jeremy1026
You can grab the integer value of a textfield using the following snippet.
您可以使用以下代码段获取文本字段的整数值。
[textField.text intValue]
Hope this helps.
希望这可以帮助。
回答by jcrowson
In Xcode, select the textField or textView and change the keyboard type to Numeric.
在 Xcode 中,选择 textField 或 textView 并将键盘类型更改为 Numeric。
Then you can get the value of whatever the user inputs into that field by doing:
然后,您可以通过执行以下操作来获取用户在该字段中输入的任何内容的值:
int value = [[textField.text] intValue];
回答by eckyzero
Here's an example of something I just used:
这是我刚刚使用的示例:
[self.phoneNumberTextField.text intValue];
[self.phoneNumberTextField.text intValue];
You have to be careful with this one. Here are the steps to get this to work
你必须小心这个。以下是使其工作的步骤
- In storyboard, select the textfield
- Open the attributes tab for the textfield in Interface Builder
- Change keyboard type to "Number Pad" (This guarantees that the user will only be able to enter ints into the textfield)
- Now, in your code, type [self.myTextField.text intValue]; (set this equal to a local variable)
- 在故事板中,选择文本字段
- 在 Interface Builder 中打开文本字段的属性选项卡
- 将键盘类型更改为“数字键盘”(这保证用户只能在文本字段中输入整数)
- 现在,在您的代码中,输入 [self.myTextField.text intValue]; (设置 this 等于局部变量)
Now, as far as WHERE in the code you'll put this, it depends. If you're trying to log in a user, I'd recommend putting this in an IBAction like 'loginButtonPressed'. Or before you transition to a new screen in 'viewWillDisappear' or even 'prepareForSegue'.
现在,就您将在代码中的位置而言,这取决于。如果您要尝试登录用户,我建议将其放入 IBAction 中,例如“loginButtonPressed”。或者在“viewWillDisappear”甚至“prepareForSegue”中过渡到新屏幕之前。
Good luck!
祝你好运!
回答by Cyril
This is from 3 years ago, I tried using that and didn't work and so I find this working for me
这是 3 年前的,我尝试使用它但没有用,所以我发现这对我有用
int i = textfield.text.intValue;
int i = textfield.text.intValue;
回答by Omar Freewan
You can get by textfield.textthis will return the text value of that text field , now to convert it to number
您可以通过textfield.text这将返回该文本字段的文本值,现在将其转换为数字
NSNumber * mynumber = [NSNumber numberWithInt:[[textfield. text] intValue]];
NSNumber * mynumber = [NSNumber numberWithInt:[[textfield. 文本] intValue]];
nslog(@"%@",mynumber);
nslog(@"%@",mynumber);