在我的 iOS 操作表中,我显示了 JSON 字典中的名称:
[
{ "Name": "Doctor for Disease AAA",
"Doctor_id": "21"
},
{ "Name": "Doctor for Disease BBB",
"Doctor_id": "22"
},
{ "Name": "Doctor for Disease AAA",
"Doctor_id": "25"
}
]
因此,在按钮单击委托(delegate)上,我可以获得按钮索引 并可以获取相应的“姓名”和“Doctor_id”。这工作正常。
但现在似乎“UIActionSheet”已被弃用,我必须使用“UIAlertController”。由于我有大量数据,我正在遍历我的数组值并调用警报 Controller 处理程序(因此所有按钮单击都是一个函数)。但是我怎样才能从 UIAlertController 获取按钮索引,以便我可以同时获取“姓名”和“Doctor_id”。
请帮帮我。
最佳答案
这里有多种可能性。
find 获取UIAlertAction 索引find 让您找到数组中对象的索引。您可以使用它来查找 action 的索引(作为 UIAlertAction 的处理程序的参数传递,它是 UIAlertAction 本身)在所有操作的 alert.actions 数组中。
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet)
let closure = { (action: UIAlertAction!) -> Void in
let index = find(alert.actions as! [UIAlertAction], action)
println("Index: \(index)")
}
alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure))
alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure))
alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure))
alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in
println("User cancelled.")
})
self.presentViewController(alert, animated: true) {}
创建一个带有您选择的参数的闭包(此处为 Int)并返回一个捕获该参数的闭包,以便您可以使用它
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet)
let closure = { (index: Int) in
{ (action: UIAlertAction!) -> Void in
println("Index: \(index)")
}
}
alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure(0)))
alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure(1)))
alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure(2)))
alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure(3)))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in
println("User cancelled.")
})
self.presentViewController(alert, animated: true) {}
这样你就有了一个函数(闭包)为你的 UIAlertAction 处理程序生成闭包,除了它们捕获不同的对象(不同的 Int在这里)。
此解决方案的真正优点在于您可以捕获任何内容。您甚至可以捕获代表您的医生的假想 Doctor 对象,或直接捕获医生 ID 等!
但通常您会使用 for 循环来添加您的 Action ,那么为什么不利用它,再加上利用闭包和它们捕获变量的事实,来制作一个很好的函数,它将直接告诉你选择的医生ID?
func testMyAlert() {
let doctors = [
["Name": "Doctor for Disease AAA", "Doctor_id": "21"],
["Name": "Doctor for Disease BBB", "Doctor_id": "22"],
["Name": "Doctor for Disease AAA", "Doctor_id": "25"]
]
chooseDoctor(doctors) { selectedDocID in
if let docID = selectedDocID {
println("User selected doctor with ID \(docID)")
} else {
println("User cancelled, no doctor selected")
}
}
}
func chooseDoctor(doctors: Array<[String:String]>, completion: Int?->Void) {
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet)
for doc in doctors {
let action = UIAlertAction(title: doc["Name"]!, style: UIAlertActionStyle.Default) { _ in
// On selecting this action, get the doctor's ID, convert it to an Int, and return that.
completion(doc["Doctor_id"]?.toInt())
}
alert.addAction(action)
}
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { _ in completion(nil) } )
self.presentViewController(alert, animated: true) {}
}
关于ios - UIAlertController 'UIAlertAction' 标签/用户数据或 Swift 中的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30286395/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer