xcode 如何设置字符串值等于 UITextField?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5756592/
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
How to set string value equal to UITextField?
提问by Guest
I'm trying to make it so you type in a number in a text field, then you press a button. A string is then equal to the text field and a label is then equal to the string. The problem is it crashes when i pres the button. What am I doing wrong?
我正在尝试让您在文本字段中输入一个数字,然后按下一个按钮。然后一个字符串等于文本字段,然后一个标签等于该字符串。问题是当我按下按钮时它会崩溃。我究竟做错了什么?
from the .h file:
从 .h 文件:
@interface TextGameViewController : UIViewController
{
IBOutlet UITextField *input;
IBOutlet UILabel *line11;
NSString *l11;
}
-(IBAction)button;
from the .m file:
从 .m 文件:
-(IBAction)button
{
[input resignFirstResponder];
l11 = ([input.text stringValue]);
line11.text = [[NSString alloc] initWithFormat:@"%2.f", l11];
}
采纳答案by Ahmad Kayyali
l11 is already a NSString
, change this line of code from:
l11 已经是一个NSString
,将这行代码从:
l11 = ([input.text stringValue]);
To
到
l11 = input.text;