【发布时间】:2016-11-08 17:22:37
【问题描述】:
所以我有一个 UItextfield 和一个 UIButton 我想知道在实例变量中按下按钮后是否有一种方法可以保存文本,以便我可以在我的其他类中使用它。 我想做的事情是让用户在屏幕 1 的文本字段中输入一些文本,然后当用户点击按钮时,我想将他带到屏幕 2 但是在该屏幕 2 的类中我想知道什么文本用户输入以便相应地显示数据。 现在我只有一个按钮的操作函数,我访问其中的文本并将其保存到实例变量中,但是当我在另一个类中调用它时它是空的,因为我将它初始化为空。所以有人可以帮助我并告诉我如何去做。 谢谢!
这是第一个屏幕的代码
var searchRecipe = ""
@IBAction func SearchButton(sender: UIButton) {
if let recipe = RecipeSearchBar.text {
searchRecipe = recipe
}
}
这是第二个屏幕的代码。我已经在这个屏幕的第一个屏幕上连接了按钮,所以当用户点击按钮时,他会到达这里。
var recipeName:[String] = []
var imageURL:[String] = []
var timeInSeconds:[Float] = []
func apiCall()
{
//search recipe API call
var searchRecipe = ""
searchRecipe = RecipesViewController().searchRecipe
print (searchRecipe) //prints nothing
endpoint = "http://api.yummly.com/v1/api/recipes?_app_id=apiId&_app_key=apiKey&q=\(searchRecipe)" //this is where I want to use the searchRecipe text from class 1
}
【问题讨论】:
-
请展示一些代码,向我们展示您是如何创建和展示您的第二个视图的。此外,这个问题已经有很多答案了,所以你可能会发现你的答案已经被关闭,除非它得到很大改进。
标签: swift uibutton uitextfield