0

I am not much of a programmer and I definitely am not skilled at user interface stuff. Currently I am helping build an iPhone app until we get someone more equipped to build it, but for right now I am having an issue with getting access to a UITextfield.

It is a login page with a username and password. I have three separate storyboard files (this was at the request of another programmer that I am working with). I created three separate classes. I have the LoginPage storyboard and the LoginClass. I have named the UITextfields: Username & Password.

There is an api and a method, both of which require the Username & Password (strings for both the api and method). Here is what I have:

public partial class LoginClass : ViewController
{
    private static APICore api = new APICore();

    public LoginClass (IntPtr handle) : base (handle)
    {
    }

    public void LoginButtonClicked(UIButton sender)
    {
        LaunchLoginAPIAsync(sender);
    }

    private async void LaunchLoginAPIAsync(UIButton sender)
    {
        string name = Username.Text;
        string pass = Password.Text;

        UserLogin user = await api.Login(name, pass);

        if (user.isInvalidUsername())
        {
            //Create Alert
            var okAlertController = UIAlertController.Create("Error", " Invalid Username. Please try again with a correct username or register using the link below", UIAlertControllerStyle.Alert);
            //Add Action
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

            // Present Alert
           // PresentViewController(okAlertController, true, null);
        }
        else if (user.isWrongPassword())
        {
            //Create Alert
            var okAlertController = UIAlertController.Create("Error", "Invalid Password", UIAlertControllerStyle.Alert);
            //Add Action
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            // Present Alert
         //   PresentViewController(okAlertController, true, null);
        }
        else if (user.isLoginSuccessful())
        {
            // Create Alert
            var okAlertController = UIAlertController.Create("Logged in!", "Success", UIAlertControllerStyle.Alert);
            //Add Action
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
      //      PresentViewController(okAlertController, true, null);
        } 
    }
}

The error I am getting is:

"ViewController.Username is not accessible due to its protection level" (and the same with Password).

SHR
  • 7,940
  • 9
  • 38
  • 57
  • 1
    If access modifier for `ViewController.Username` and `ViewController.Password` is private or there is nothing specified then make it either `protected` or `public`. If it doesn't help post your `ViewController`. – Sumit raj Jul 23 '18 at 02:21
  • @Sumitraj I am not even sure how to access ViewController.Username. In the storyboard, I set the name of the UITextfield as Username. It worked fine when I had it in the original ViewController, but once I've created the new Class to associate with the separate Storyboard, I've been getting this error. I can post the ViewController, but do you want the ViewController.cs file or the Designer file? –  Jul 23 '18 at 02:28
  • Post `ViewController.cs` file. You don't need to post whole just where `username` and `password` is referenced – Sumit raj Jul 23 '18 at 02:32
  • I must not be doing something wrong because I didn't include them in that original file, but I could use protected string.. except then it says "A field initializer cannot reference the non-static field, method, or property 'ViewController.Password'" –  Jul 23 '18 at 02:41
  • It's because of `string name = Username.Text; string pass = Password.Text;`. Take a look at this https://stackoverflow.com/questions/14439231/a-field-initializer-cannot-reference-the-nonstatic-field-method-or-property – Sumit raj Jul 23 '18 at 02:45
  • I don't think I am understanding very well. I have the ViewController.cs and then I have the LoginClass.cs I have my Storyboard connected to the LoginClass.cs just so the ViewController.cs would not be cluttered. The LoginClass.cs is what I've posted above. The ViewController.cs has nothing, but when I add anything like protected string or dynamic string or anything like that, it gives me all sorts of errors. –  Jul 23 '18 at 02:57
  • Please post `ViewController` . I cannot assume wt's in dere – Sumit raj Jul 23 '18 at 03:02
  • This is all I have in there: public ViewController(IntPtr handle) : base(handle) { } public override void ViewDidLoad() { base.ViewDidLoad(); } public override void DidReceiveMemoryWarning() { base.DidReceiveMemoryWarning(); // Release any cached data, images, etc that aren't in use. } –  Jul 23 '18 at 03:26
  • It would be awesome if you could provide a [mcve]. – mjwills Jul 23 '18 at 03:55

0 Answers0