gpt4 book ai didi

ios - Xamarin ios - 从动态 UIAlertController 获取所选项目的值时出错

转载 作者:行者123 更新时间:2023-11-29 01:19:43 25 4
gpt4 key购买 nike

我有一个创建年份的列表,我正在使用这个列表动态创建一个 UIAlertController。

用户单击 TableView 中一行的按钮,这会启动 UIAlertController,用户可以选择年份。

我的挑战是:

  1. 每次我从 Controller 中选择年份时,它都会选择取消按钮。

获取所选操作索引的正确方法是什么?

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

最佳答案

解决方案非常简单。

只需像这样访问所选操作的 Title 属性:

改变这个

string year = actionSheetAlert.Actions.ElementAt(i).ToString();

为此

string year = action.Title;

关于ios - Xamarin ios - 从动态 UIAlertController 获取所选项目的值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com