修改另一个视图控制器swift中的一个变量 [英] Modifing one variable from another view controller swift
问题描述
我正在Swift中开发一个应用程序,用一个主旨,告诉人们各种货币的比特币价格。要选择货币,用户可以从具有UITableView的视图控制器中的列表中进行选择。这是currencyViewController,它是从我的主屏幕viewController呈现的。
I am developing an app in Swift that, in gist, tells people the price of Bitcoin in various currencies. To select the currency, the user chooses from a list in a view controller with a UITableView. This is currencyViewController, and it is presented from my main screen, viewController.
我想要发生的是,当用户解雇currencyViewController时,它将一个字符串传递给一个主视图中的UIButton。
What I want to happen is that, when the user dismisses currencyViewController, it passes a string to a UIButton in the main viewController.
这是应该传递数据的prepareForSegue函数:
Here's the prepareForSegue function that should pass the data:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "presentCurrency") {
currencySelector.setTitle("\currencySelected", forState: UIControlState.Normal)
}
}
CurrencySelector是主viewController中的UIButton,而currencySelected是第二个视图控制器中的变量currencyViewController。
CurrencySelector is a UIButton in the main viewController, and currencySelected is a variable in the second view controller, currencyViewController.
它给出错误文字中无效的转义序列
It gives the error "Invalid Escape Sequence In Literal"
所以,我把它缩小到两个问题之一:
So, I've narrowed it down to one of two issues:
-
来自viewCo的变量无法从currencyViewController看到ntroller。如果是这样,我如何从CurrencyViewController修改CurrencySelector的文本?
The variables from viewController can't be "seen" from currencyViewController. If so, how can I modify the text of CurrencySelector from CurrencyViewController?
出于某种原因,当用户退出推送的CurrencyViewControler时,不会调用prepareForSegue。
For some reason, when the user exits the pushed CurrencyViewControler, prepareForSegue isn't called.
这里发生了什么?谢谢,道歉 - 我只是一个快速的新手。
What is going on here? Thanks, and apologies - I am but a swift newbie.
推荐答案
2 - 当你推新视图时调用prepareForSegue控制器通过segue,但不是你解雇它。解雇时不会调用segue。
2 - "prepareForSegue" is called when you push a new view controller via the segue, but not when you dismiss it. No segue is called upon dismissal.
1 - 执行此操作的好方法是委托模式。
1 - A good way to do this would be the delegate pattern.
因此主视图控制器将是currencyViewController的委托,并在该控制器被解除时收到消息。
So the main view controller would be the delegate for the currencyViewController, and would receive a message when that controller is dismissed.
在currencyViewController文件的开头,您准备委托:
In the start of the currencyViewController file you prepare the delegate:
protocol CurrencyViewControllerDelegate {
func currencyViewControllerDidSelect(value: String)
}
并向currencyViewController添加一个变量:
and you add a variable to the currencyViewController:
var delegate : CurrencyViewControllerDelegate?
现在,mainViewController必须符合该协议并回答该函数:
Now, the mainViewController has to conform to that protocol and answer to that function:
class MainViewController : UIViewController, CurrencyViewControllerDelegate {
//...
func currencyViewControllerDidSelect(value: String) {
//do your stuff here
}
}
一切准备就绪。最后一步,在prepareForSegue(MainViewController)中,您将设置currencyViewController的委托:
And everything is prepared. Last steps, in prepareForSegue (MainViewController), you will set the delegate of the currencyViewController:
var currencyVC = segue.destinationViewController as CurrencyViewController
currencyVC.delegate = self;
当用户选择currencyViewController中的值时,只需在委托中调用该函数:
And when the user selects the value in currencyViewController, just call that function in the delegate:
self.delegate?.currencyViewControllerDidSelect("stuff")
可能有点复杂,但这是一个非常有用的模式:)这里有一个很好的教程,如果你需要更多信息:
A bit complex maybe, but it's a very useful pattern :) here is a nice tutorial with more info if you want it:
http://www.raywenderlich .com / 75289 / swift-tutorial-part-3-tuples-protocols-delegates-table-views
这篇关于修改另一个视图控制器swift中的一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!