Xamarin ios - 从动态 UIAlertController 获取所选项目的值时出错
Xamarin ios - Error getting value of selected item from a dynamic UIAlertController
我有一个创建年份的列表,我正在使用这个列表动态创建一个 UIAlertController。
用户单击表视图中一行的按钮,这会启动 UIAlertController,并且用户每年可以 select。
我的挑战是:
- 每次我从控制器 select 一年,它 select 取消按钮。
获取 selected 操作索引的正确方法是什么?
// Create a new Alert Controller
actionSheetAlert =
UIAlertController.Create ("", "SELECT YEAR", UIAlertControllerStyle.ActionSheet);
try {
for (int i = 0; i < years.Count; i++) {
// Add Actions
actionSheetAlert.AddAction (
UIAlertAction.Create (years [i].ToString (), UIAlertActionStyle.Default,
(action) => {
string year = actionSheetAlert.Actions.ElementAt(i).ToString();
Console.WriteLine ("Selected year:" + year);
this.DismissViewController (true, null);
}
));
}
} catch (Exception ex) {
Console.WriteLine (ex.Message + ex.StackTrace);
}
actionSheetAlert.AddAction (
UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel,
(action) => {
this.DismissViewController (true, null);
}
));
this.PresentViewController (actionSheetAlert, true, null);
解决方法很简单。
只需像这样访问所选操作的标题 属性:
改变这个
string year = actionSheetAlert.Actions.ElementAt(i).ToString();
对此
string year = action.Title;
我有一个创建年份的列表,我正在使用这个列表动态创建一个 UIAlertController。
用户单击表视图中一行的按钮,这会启动 UIAlertController,并且用户每年可以 select。
我的挑战是:
- 每次我从控制器 select 一年,它 select 取消按钮。
获取 selected 操作索引的正确方法是什么?
// Create a new Alert Controller
actionSheetAlert =
UIAlertController.Create ("", "SELECT YEAR", UIAlertControllerStyle.ActionSheet);
try {
for (int i = 0; i < years.Count; i++) {
// Add Actions
actionSheetAlert.AddAction (
UIAlertAction.Create (years [i].ToString (), UIAlertActionStyle.Default,
(action) => {
string year = actionSheetAlert.Actions.ElementAt(i).ToString();
Console.WriteLine ("Selected year:" + year);
this.DismissViewController (true, null);
}
));
}
} catch (Exception ex) {
Console.WriteLine (ex.Message + ex.StackTrace);
}
actionSheetAlert.AddAction (
UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel,
(action) => {
this.DismissViewController (true, null);
}
));
this.PresentViewController (actionSheetAlert, true, null);
解决方法很简单。
只需像这样访问所选操作的标题 属性:
改变这个
string year = actionSheetAlert.Actions.ElementAt(i).ToString();
对此
string year = action.Title;