Xamarin iOS UIAlertController 未显示
Xamarin iOS UIAlertController not showing
在我的程序中,当您点击第一个视图控制器上的 'begin' 按钮时,它会将您带到一个名为 VCLoadLocalData 的新视图控制器。当该视图控制器加载时,它将检查某个 txt 文件是否存在,如果存在,它将显示一个需要用户输入的警报,如果不存在,它将显示另一个警报。目前,UIAlertController 没有出现;我已经用 UIAlertView 试过了,但我不知道如何让用户响应 return 或让它触发一个方法。这是我目前拥有的 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);
}
}
}
在方法ViewDidLoad
中,视图不是initialized.You可以将你的代码移动到方法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);
}
}
请注意,这段代码可能会执行多次,每当 VCLoadLocalData
appears.So 如果您只是检查一次 time.You 可以改进代码。
. . .
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);
}
}
}
在我的程序中,当您点击第一个视图控制器上的 'begin' 按钮时,它会将您带到一个名为 VCLoadLocalData 的新视图控制器。当该视图控制器加载时,它将检查某个 txt 文件是否存在,如果存在,它将显示一个需要用户输入的警报,如果不存在,它将显示另一个警报。目前,UIAlertController 没有出现;我已经用 UIAlertView 试过了,但我不知道如何让用户响应 return 或让它触发一个方法。这是我目前拥有的 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);
}
}
}
在方法ViewDidLoad
中,视图不是initialized.You可以将你的代码移动到方法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);
}
}
请注意,这段代码可能会执行多次,每当 VCLoadLocalData
appears.So 如果您只是检查一次 time.You 可以改进代码。
. . .
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);
}
}
}