Afsara
Afsara

Reputation: 1461

Need to get placeholder text of UITextField

Is it possible to get the placeholder value of UITextField, which is designed in storyboard?

I need to validate the form which is having more textfields, so instead of giving static value, I want to give placeholder string of particular textfield as alert message.

Upvotes: 0

Views: 4423

Answers (5)

user5724238
user5724238

Reputation: 27

For placeholder first you should create IBOUTLET for textfield and then do:

 if let str = textfiled.placeholder {

        if !str.isEmpty {
            //do something
        }
    }

Upvotes: 1

Jigar Tarsariya
Jigar Tarsariya

Reputation: 3237

Simply write like below code,

if((self.myTextFiled!.placeholder! as NSString).isEqualToString("AnyString"))
{
     // your code here  
} 

Upvotes: 0

Pavan Gandhi
Pavan Gandhi

Reputation: 1749

Its work ...

For getting placeholder text you need to wire up your textfield with its IBOutlet object like,

@IBOutlet var txtField : UITextField?

Now you set Placeholder Statically or dynamically

self.txtField!.attributedPlaceholder = NSAttributedString(string:"Good", attributes:[NSForegroundColorAttributeName: UIColor.grayColor()])

After that anywhere you can simply get its placeholder text and compare or validate ur textfield like

if((self.txtField!.placeholder! as NSString).isEqualToString("Good"))
{
    // Do here Whatever you want   
}
else if(self.txtField!.placeholder!.isEmpty == true)
{
   // check its empty or not      
} 

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53830

The UITextField class has a placeholder property. To reference the UITextField in code, you'll need to create an outlet for it in your view controller.

If you named the outlet myTextField, you could reference the placeholder like this:

let placeholder = self.myTextField.placeholder

Upvotes: 0

Adam
Adam

Reputation: 26907

Use the placeholder property:

textField.placeholder

Upvotes: 5

Related Questions