Ios 从另一个视图控制器访问更新的变量值
我有一个包含三个不同视图控制器的项目: ViewController(这是用户输入值的地方)、LoadingViewController(加载屏幕)和AnswerViewController(我希望在第一个视图控制器中加载更新值的结果) 以下是ViewController中的代码:Ios 从另一个视图控制器访问更新的变量值,ios,xcode,swift,Ios,Xcode,Swift,我有一个包含三个不同视图控制器的项目: ViewController(这是用户输入值的地方)、LoadingViewController(加载屏幕)和AnswerViewController(我希望在第一个视图控制器中加载更新值的结果) 以下是ViewController中的代码: var calculatedRatio: Double? @IBOutlet weak var boysCountTextField: UITextField! @IBOutlet weak var girlsCo
var calculatedRatio: Double?
@IBOutlet weak var boysCountTextField: UITextField!
@IBOutlet weak var girlsCountTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func calculateButtonTapped(sender: UIButton) {
if boysCountTextField.text != "" && girlsCountTextField.text != "" {
calculateRatio()
self.performSegueWithIdentifier("loadingAnswer", sender: self)
}
}
func calculateRatio() {
let boysCount = Double(boysCountTextField.text!)!
let girlsCount = Double(girlsCountTextField.text!)!
let currentRatio = boysCount / girlsCount
calculatedRatio = currentRatio
print(calculatedRatio)
}
如您所见,在这个视图控制器中,我在顶部创建了一个名为calculatedRatio
的变量,它是一个可选的双精度变量。在同一视图控制器中,一旦按下按钮,它将在底部运行一个名为calculateRatio()
的函数,该函数将更新calculatedRatio
值
我的问题是,我如何能够在我没有直接进入的另一个视图控制器(AnswerViewController)中访问该变量的新值
我已经尝试在AnswerViewController中创建ViewController的实例,但是复制变量时使用其初始值(在本例中为nil),而不是
calculateRatio()给出的新值
函数。因此,在处理复杂方法时,您希望显示一个作为自己的视图控制器的加载屏幕。我假设您希望在导航堆栈上抛出一个加载屏幕,而不是进行某种模式覆盖是有原因的。我还假设计算必须在第一视图控制器上进行(与加载屏幕相反),并且没有更好的方法将计算从别处提取出来,这是有原因的。你为什么不试试呢?您可以在LoadingViewController中侦听计算是否完成,并将更新值作为对象传递 您可以使用iOS应用程序的委派模式。在这里,我试图解决你的问题。我对输入ViewController类做了一些更改,以实现AnswerViewController的委托
class ViewController: UIViewController,AnswerVCProtocol {
var calculatedRatio: Double?
@IBOutlet weak var boysCountTextField: UITextField!
@IBOutlet weak var girlsCountTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func calculateButtonTapped(sender: UIButton) {
if boysCountTextField.text != "" && girlsCountTextField.text != "" {
calculateRatio()
self.performSegueWithIdentifier("loadingAnswer", sender: self)
}
}
func calculateRatio() {
let boysCount = Double(boysCountTextField.text!)!
let girlsCount = Double(girlsCountTextField.text!)!
let currentRatio = boysCount / girlsCount
calculatedRatio = currentRatio
print(calculatedRatio)
}
// MARK: AnswerViewController Delegate
func getUpdateRadio()-> Double?{
return self.calculatedRatio
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Please check you identifier as you given in storyboard
if segue.identifier == "loadingAnswer"{
let ansVc = segue.destinationViewController as! AnswerViewController
ansVc.delegate = self
}
}
}
这是协议
protocol AnswerVCProtocol{
func getUpdateRadio()-> Double?
}
现在我猜你的回答是这样的。这里我只展示如何通过委托访问变量
class AnswerViewController: UIViewController {
var delegate: AnswerVCProtocol?
override func viewDidLoad() {
<#code#>
}
// To receive update value call this function when you needed
// Receive Update Value from your Input ViewController
func updateRadio(){
if let _ = self.delegate{
let updateRatio = delegate?.getUpdateRadio()
}
}
}
class AnswerViewController:UIViewController{
var代表:应答协议?
重写func viewDidLoad(){
}
//要接收更新值,请在需要时调用此函数
//从输入ViewController接收更新值
func updateRadio(){
如果让uu=self.delegate{
让updateRatio=delegate?.getUpdateRadio()
}
}
}
概述
AnswerVCProtocol
class ViewController: UIViewController,AnswerVCProtocol {
var calculatedRatio: Double?
@IBOutlet weak var boysCountTextField: UITextField!
@IBOutlet weak var girlsCountTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func calculateButtonTapped(sender: UIButton) {
if boysCountTextField.text != "" && girlsCountTextField.text != "" {
calculateRatio()
self.performSegueWithIdentifier("loadingAnswer", sender: self)
}
}
func calculateRatio() {
let boysCount = Double(boysCountTextField.text!)!
let girlsCount = Double(girlsCountTextField.text!)!
let currentRatio = boysCount / girlsCount
calculatedRatio = currentRatio
print(calculatedRatio)
}
// MARK: AnswerViewController Delegate
func getUpdateRadio()-> Double?{
return self.calculatedRatio
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Please check you identifier as you given in storyboard
if segue.identifier == "loadingAnswer"{
let ansVc = segue.destinationViewController as! AnswerViewController
ansVc.delegate = self
}
}
}
我该怎么做?-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender{if([[segue identifier]isequaltstring:@“loadingAnswer]”){LoadingViewController*destinationViewController=[segue destinationViewController];destinationViewController.calculateratio=self.calculatedRatio}重写func prepareforsgue(segue:UIStoryboardSegue!,sender:AnyObject!){if(segue.identifier==“loadingAnswer”){let vc:LoadingViewController=segue!。destinationViewController作为LoadingViewController.calculatedRatio=self.calculatedRatio}您尝试过使用委派吗?