UITextField 和 UITextView 的内容到 NSString

发布于 2024-07-21 12:48:45 字数 527 浏览 2 评论 0原文

我设置了一个 UITextView 和 2 个 UITextField 。 当点击屏幕的空白部分时,UITextView 会放弃第一响应者状态,对于 2 个 UITextField 也是如此,此外,对于这 2 个 UITextField,返回键也会放弃第一响应者状态。 所有 3 个都在接口中声明。

我想将所有这些内容获取到单独的 NSString 和/或学习如何将它们直接输入到类似的内容中:

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?var1=%@&var2=%@&var3=%@", *content of UITextView*, *content of UITextField*, *content of UITextField*];

我知道,这是一个非常基本的问题,但我几乎是一个新手。 如果我学会了如何做到这一点,我可能就能从那里开始。

干杯

(已编辑)

I have a UITextView and 2 UITextField set up. UITextView resigns first responder status when empty part of the screen is tapped, the same for the 2 UITextField, plus for these 2, the return key also resigns first responder status. All 3 are declared in interface.

I would like to get the contents of all of these to individual NSString and/or learn how to enter them directly into something like:

NSString *urlstr = [[NSString alloc] initWithFormat:@"http://server.com/file.php?var1=%@&var2=%@&var3=%@", *content of UITextView*, *content of UITextField*, *content of UITextField*];

This is a very basic question, i know, but i'm pretty much a novice. If i learn how to do this i'll probably be able to pick up from there.

cheers

(edited)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-07-28 12:48:45

UITextField 和 UITextView 都有一个 text 属性,可用于检索字符串值。 例如,

NSString *string = [NSString stringWithFormat:@"%@, %@", textField.text, textView.text];

请记住,您可能想要检查字符串以制作在将它们放入 URL 之前,请确保它们不为空或包含无效字符。

UITextField and UITextView both have a text property that you can use to retrieve the string values. For example,

NSString *string = [NSString stringWithFormat:@"%@, %@", textField.text, textView.text];

Keep in mind you'll probably want to examine the strings to make sure they're not empty or contain invalid characters before putting them into a URL.

风筝有风,海豚有海 2024-07-28 12:48:45

接受的答案很好,我只是想添加以下内容以扩展在 iOS 中抓取文本的外观。

请参阅我设计的以下代码的 textInRange: 方面使用一个函数来确定文本是否是 UITextField、UITextView 或任何其他符合 UITextInput 协议的类。

//handle text container object length whether it's a UITextField, UITextView et al
NSUInteger LengthOfStringInTextInput(NSObject<UITextInput> *textContainer)
{
    UITextPosition *beginningOfDocument = [textContainer beginningOfDocument];
    UITextPosition *endOfDocument =       [textContainer endOfDocument];
    UITextRange    *fullTextRange =       [textContainer textRangeFromPosition:beginningOfDocument 
                                                                    toPosition:endOfDocument];
    return [textContainer textInRange:fullTextRange].length;
}

通过将返回类型更改为 NSString 并删除 .length,您可以在任何类上拥有文本属性的功能。

The accepted answer is good, I just wanted to add the following for an expanded look at grabbing text in iOS.

See the textInRange: aspect of the below code that I devised to use one function to determine the text whether it's a UITextField, UITextView or any other class that complies with the UITextInput protocol.

//handle text container object length whether it's a UITextField, UITextView et al
NSUInteger LengthOfStringInTextInput(NSObject<UITextInput> *textContainer)
{
    UITextPosition *beginningOfDocument = [textContainer beginningOfDocument];
    UITextPosition *endOfDocument =       [textContainer endOfDocument];
    UITextRange    *fullTextRange =       [textContainer textRangeFromPosition:beginningOfDocument 
                                                                    toPosition:endOfDocument];
    return [textContainer textInRange:fullTextRange].length;
}

By changing the return type to NSString and removing .length you could have the functionality of the text property on any class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文