1

I am creating a View Controller that records the Phone Number or Email adress of the user, and I want the placeholder of the textfield to change when they select either the "Phone" button, or the "Email" button.

This is just the button that is supposed to change the placeholder

 void PhoneSelectBtn(object sender, EventArgs e)
    {
        EmailPhoneBox.AttributedPlaceholder = new NSAttributedString("Phone");
    }

and every time I run the application, it crashes and this is the error that I get: Objective-C exception thrown. Name: NSUnknownKeyException Reason: [<UIViewController 0x7f813ec3b870> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key EmailPhoneBox. Native stack trace:

I have tried other options, such as using a segmented control (obviously it is a little different then setting up a button), and I get the same result. I have changed button methods, classes, and the same result occurs every single time. There are no unnecessary events lingering that aren't attached to anything either. Out of ideas. If one could explain step by step what to do, that would be great. I am sure that it isn't a hard thing to do, but I am just learning, and am finding it hard to find applicable documentation on small things like this. Thanks, Josh

J Buff
  • 35
  • 6
  • Possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"](https://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v) – Access Denied Aug 31 '18 at 03:08
  • For more specific answer please post runnable sample which demonstrates the problem. – Access Denied Aug 31 '18 at 03:11

1 Answers1

2

It seems that just like with many other Xamarin features which seem like they should be added but aren't, this one needs a custom renderer.

See this link for more.

Another option is

Set the placeholder string to your Editor's Text in Xaml Then in Code behind file:

InitializeComponent();

var placeholder = myEditor.Text;

myEditor.Focused += (sender, e) =>
{
     // Set the editor's text empty on focus, only if the place 
     // holder is present
     if (myEditor.Text.Equals(placeholder))
     {
          myEditor.Text = string.Empty;
          // Here You can change the text color of editor as well
          // to active text color
     }
};

myEditor.Unfocused += (sender, e) => 
{
     // Set the editor's text to place holder on unfocus, only if 
     // there is no data entered in editor
    if (string.IsNullOrEmpty(myEditor.Text.Trim()))
    {
        myEditor.Text = placeholder;
        // Here You can change the text color of editor as well
        // to dim text color (Text Hint Color)
    }
};
jamesfdearborn
  • 769
  • 1
  • 10
  • 26