简体   繁体   中英

Xamarin iOS UIAlertController not showing

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);
        }
    }
}

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);
     }
   }

}

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