1

I am a beginner in swift. I have

class A : UIViewController { var textInput: UITextInput init(textInput: UITextInput) { self.textInput = textInput } func getText() -> String() { /// Here I need to get the current text from textInput } } 

How to get it ? Help please. Thanks in advance!!!!

1
  • Hi all.. I got finally Commented Oct 30, 2015 at 14:01

4 Answers 4

4

Swift 3:

let start = sender.beginningOfDocument let end = sender.endOfDocument let range = sender.textRange(from: start, to: end)! let trimmedText = sender.text(in: range) sender.replace(range, withText: "new text") 
Sign up to request clarification or add additional context in comments.

Comments

1

I got it.

let start: UITextPosition = self.textInput.beginningOfDocument let end: UITextPosition = self.textInput.endOfDocument let range: UITextRange = textInput!.textRangeFromPosition(start!, toPosition: end!)! textInput!.textInRange(range!) // for get text textInput.replaceRange(range!, withText: "some text") // to write text 

Comments

0
import Foundation import UIKit public extension UITextInput { public var text: String { get { text(in: textRange(from: beginningOfDocument, to: endOfDocument)!) ?? "" } set(value) { replace(textRange(from: beginningOfDocument, to: endOfDocument)!, withText: value) } } } 

Comments

-1

Your textInput variable is never declared properly. You might want to consider learning more general practices of Swift before asking specific questions.

Proper declaration of textInput would look like:

class A : UIViewController { var textInput: UITextInput 

3 Comments

I am passing a textView or textField to this init(). They were initialising in other controllers. Can you help me how to use a UITextInput ?
Why not use a UITextField and get the text with textInput.text?
Right, why not use a UITextField?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.