【问题标题】:Swift 2.2 : Pass value between view controllers iOSSwift 2.2:在视图控制器 iOS 之间传递值
【发布时间】:2017-01-01 22:25:13
【问题描述】:

我正在尝试在两个视图控制器之间传递值,但失败了。 当按下第一个视图控制器中的按钮时,第二个视图控制器(即表格视图控制器)会以模态的方式呈现。

当用户点击第二个视图控制器中存在的任何单元格时,它将被关闭,并且单元格的值设置为第一个视图控制器中存在的按钮。

下面是我用过的代码:

ForgotPasswordViewController

class ForgotPasswordViewController: UIViewController {

    var securityQuestion = ""

    @IBOutlet weak var btnSecurityQuestion: UIButton!

    //MARK: - viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    var secQuestionAccessor: (String) {
        get{
            return securityQuestion
        }set(param) { print("param : \(param)") //Value is correctly printed in console
           securityQuestion = param
            print("securityQuestion : \(securityQuestion)")  //Value is correctly printed in console
        }
    }

    //MARK: - viewWillAppear
    override func viewWillAppear(animated: Bool) {        
        . . .

        print("secQuestionAccessor : \(secQuestionAccessor)") //Value is empty here

        if secQuestionAccessor == "" {
            btnSecurityQuestion.setTitle("Select", forState: UIControlState.Normal)
        }
        else {
            btnSecurityQuestion.setTitle(secQuestionAccessor, forState: UIControlState.Normal)
        }
        . . .
    }
  . . .
}

SecurityQuestionViewController:

class SecurityQuestionViewController: UITableViewController {
  . . .

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let objForgotPassword = ForgotPasswordViewController()
        print("\(responseQuestion.objectAtIndex(indexPath.row) as! String)") //Value printed correctly here
        objForgotPassword.secQuestionAccessor = responseQuestion.objectAtIndex(indexPath.row) as! String
        self.dismissViewControllerAnimated(true, completion: nil)
    }
  . . .
}

我哪里出错了?还有其他解决方案吗?

这个link 帮助使用 Swift 语言创建了一个委托。 但是,app crashes when second view controller is presented using modal segue.

当委托被声明为弱并在第二个视图控制器中设置为 nil 时,它崩溃。它适用于 show segue。

如何使用模态转场来做到这一点?

【问题讨论】:

  • objForgotPassword 是全新的实例,它不是以前的视图控制器,永远不会出现在屏幕上
  • @ShadowOf 是对的。另外,您可以按照 Max Pevsner 的建议查看此帖子 stackoverflow.com/questions/6203799/…。
  • 您可以通过检查 SecurityQuestionViewController 中的 self.presentingViewController 来访问 ForgotPasswordViewController 的实例
  • @JJBoursier:请检查问题。进一步阐述。

标签: ios swift xcode storyboard segue


【解决方案1】:

向您的SecurityQuestionViewController 添加一个协议,该函数接受您想要传递给前一个viewController 的数据作为参数。在您的ForgotPasswordViewController 中实现该功能。在解除 SecurityQuestionViewController 之前在委托上调用该函数。

【讨论】:

  • 问题进一步阐述。请检查。
【解决方案2】:

好的兄弟我可以给你线索...不需要通知不需要协议...

做一件事只需从导航层次结构中获取父视图控制器的实例并访问其要在其中设置数据的属性变量

如果您正在呈现视图控制器,则通过此访问父级

ParentVC *objParentVC = self.presentingViewController

如果您正在推送视图控制器,则通过

访问父级
ParentVC *objParentVC = [[self.navigationController childViewController] objectAtIndex:self.navigationController.childViewController.count - 2];

只是为了澄清。 在这里,如果你 -2 将是你的父母。 -1 将是您当前的视图控制器。作为它的计数

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多