iOS:如何从文本字段中获取文本并将其显示在警报中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17529332/
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
iOS: How do I get text from a text field and display it in an alert?
提问by mrcoulson
guys. This is totally going to sound like I'm asking for you to do my homework for me, but I'm not. My employer FINALLY gave me this sweet new MacBook Pro. One of my tasks will include some iOS development. I'm pumped about it and I'm trying to dive right in to learning, so I'm making a silly little application that just lets me see how to interact and write some code. This morning's task is to take some text from a text field and display it in an alert. I've done lots of googling and found lots of things -- even stuff on StackOverflow -- but a lot of it is over my head or not exactly relevant. So, I'm hoping someone can show me what I've done wrong.
伙计们。这听起来完全像是我要求你为我做作业,但我没有。我的雇主终于给了我这款可爱的新 MacBook Pro。我的任务之一将包括一些 iOS 开发。我对它很感兴趣,我正在尝试直接学习,所以我正在制作一个愚蠢的小应用程序,它只是让我了解如何交互和编写一些代码。今天早上的任务是从文本字段中提取一些文本并将其显示在警报中。我做了很多谷歌搜索,发现了很多东西——甚至是 StackOverflow 上的东西——但其中很多都在我的脑海中,或者不完全相关。所以,我希望有人能告诉我我做错了什么。
Here's my code for the text field:
这是我的文本字段代码:
-(IBAction)showInputMessage:(UITextField *)textField
{
if ([textField.text isEqualToString:@""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textField.text]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
And then I connect that text field to showInputMessage and run it in the iPhone simulator, but nothing happens when I enter text and click "Enter".
然后我将该文本字段连接到 showInputMessage 并在 iPhone 模拟器中运行它,但是当我输入文本并单击“Enter”时没有任何反应。
Thanks in advance. I've only been playing with this language since last night.
提前致谢。从昨晚开始我就一直在玩这种语言。
Jeremy
杰里米
回答by Abdullah Shafique
Set Delegate for UITextView.
为 UITextView 设置委托。
First declare the delegate:
首先声明委托:
@interface YourViewController ()<UITextFieldDelegate>
Second set to self
第二套给自己
self.textView.delegate = self;
Use this method:
使用这个方法:
-(void)textViewShouldReturn:(UITextField *)textField
{
if ([textField.text isEqualToString:@""]){
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textField.text]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}
To add a property write the following code in your h file:
要添加属性,请在 h 文件中写入以下代码:
@property (weak, nonatomic) IBOutlet UITextView *textView;
To connect it your text field go to the storyboard and click its view controller. Next go to the Connections Inspector(All the way on the right). Under outlets drag the circle next to textView to your textView.
要连接它,您的文本字段转到故事板并单击其视图控制器。接下来转到连接检查器(一直在右边)。在出口下,将 textView 旁边的圆圈拖动到您的 textView。
回答by soryngod
Have you linked the UITextField
in the xib
file or the storyboard
?
I see now that you have a textField as a parameter. You won't get anything like that;
你的链接UITextField
中xib
的文件或storyboard
?我现在看到你有一个 textField 作为参数。你不会得到那样的东西;
Do this in .h
在 .h 中执行此操作
IBOutlet UITextField *textFieldTest;
then link it in the xib or storyboard
然后将其链接到 xib 或故事板中
-(IBAction)showInputMessage
{
if ([textFieldTest.text isEqualToString:@""])
{
return;
}
UIAlertView *helloEarthInputAlert = [[UIAlertView alloc]
initWithTitle:@"Name!" message:[NSString stringWithFormat:@"Message: %@", textFieldTest.text]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// Display this message.
[helloEarthInputAlert show];
}