简体   繁体   中英

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.

使用placeholder属性:

textField.placeholder

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

 if let str = textfiled.placeholder {

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

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

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      
} 

Simply write like below code,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM