1

In my program, when you hit the 'begin' button on the first view controller, it takes you to a new view controller called VCLoadLocalData. When that view controller loads, it will check to see if a certain txt file exists, if it does, it will show one alert that requires user input, if it doesn't, it will show a different alert. At the moment, the UIAlertController doesn't show up; I have tried it with a UIAlertView but I didn't know how to make the user response return or for it to fire a method. Here is the code I have so far for VCLoadLocalData:

using Foundation;
using System;
using System.IO;
using System.Threading.Tasks;
using UIKit;

namespace SixthFormFinder
{

    public partial class VCLoadLocalData : UIViewController
    {

        public VCLoadLocalData (IntPtr handle) : base (handle)
        {

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            bool doesExist = File.Exists("UserData.txt");
            if (doesExist)
            {
                //UIAlertView alert = new UIAlertView()
                //{
                //    Title = "User Data Found",
                //    Message = "A version of your data has been found, do you want to use it?"
                //};
                //alert.AddButton("Yes");
                //alert.AddButton("No");
                //alert.Show();
                var alert = UIAlertController.Create("User Data Found", "A version of your data has been found, do you want to use it?", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_Yes()));
                alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
                ShowViewController(alert, null);
            }
            else
            {

                var alert = UIAlertController.Create("No User Data", "No version of your user data was found, you will need to follow this setup", UIAlertControllerStyle.Alert);
                alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
                ShowViewController(alert, null);
            }
        }

        void Alert_Yes()
        {
            var alert = UIAlertController.Create("Yes", "Yes Test complete", UIAlertControllerStyle.Alert);
            ShowViewController(alert, null);
        }

        void Alert_No()
        {
            var alert = UIAlertController.Create("No", "No Test complete", UIAlertControllerStyle.Alert);
            ShowViewController(alert, null);
        }
    }
}
2
  • Hey.Have you solved the issue? Oct 9, 2018 at 1:11
  • yeah, worked perfectly. Thank You Oct 9, 2018 at 8:13

1 Answer 1

2

In method ViewDidLoad, the view is not initialized.You can move your code into the method ViewDidAppear .

public override void ViewDidAppear(bool animated)
{
  base.ViewDidAppear(animated);

  bool doesExist = File.Exists("UserData.txt");
  if (doesExist)
   {
     var alert = UIAlertController.Create("User Data Found", "A version of your data has been found, do you want to use it?", UIAlertControllerStyle.Alert);
     alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_Yes()));
     alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
     ShowViewController(alert, null);
   }
  else
   {

     var alert = UIAlertController.Create("No User Data", "No version of your user data was found, you will need to follow this setup", UIAlertControllerStyle.Alert);
     alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
     ShowViewController(alert, null);
    }
}

Note that this code may be executed multiple times, whenever the VCLoadLocalData appears.So if you just to check it one time.You can improve the code.

 . . .
 private bool isFirstLoad = true;
 . . .
 public override void ViewDidAppear(bool animated)
 {
   base.ViewDidAppear(animated);

   if(isFirstLoad)
   {
    isFirstLoad=false;
    bool doesExist = File.Exists("UserData.txt");
    if (doesExist)
     {
      var alert = UIAlertController.Create("User Data Found", "A version of your data has been found, do you want to use it?", UIAlertControllerStyle.Alert);
      alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_Yes()));
      alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
      ShowViewController(alert, null);
     }
    else
     {

      var alert = UIAlertController.Create("No User Data", "No version of your user data was found, you will need to follow this setup", UIAlertControllerStyle.Alert);
      alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (UIAlertAction obj) => Alert_No()));
      ShowViewController(alert, null);
     }
   }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.