如何将UITextField的内容传递给UITabbarController链接的另一个UITableViewController

时间:2016-08-29 00:38:02

标签: ios swift uitabbarcontroller

我是swift 3中iOS开发的新手,我遇到了一些麻烦。 如下图所示,我有2个UITableViewControllers,第一个带有文本字段。这两个tableView由tabbarController链接。我想恢复textfield的内容,以便在secondTableViewController中使用它。 我不知道该怎么做,关于数据传递的最常见的教程用按钮动作解释,但是如果可能的话,我想在更改tableView时简单地恢复数据。

我希望我的问题清楚。

这是我的第一个UITableViewController:

class InvestmentTableViewController: UITableViewController, UITabBarControllerDelegate

var someVariable: Float?

override func viewDidLoad()
{  
    super.viewDidLoad()

    self.tabBarController?.delegate = self
}

  func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UITableViewController) -> Bool
{

    let selectedVC:ExtraChargesTableViewController = viewController as! ExtraChargesTableViewController
    selectedVC.totalInvest = self.someVariable

    return true

}

在我的其他tabView

class ExtraChargesTableViewController: UITableViewController
{

 var totalInvest: Float?

我不知道如何解决这个问题。 enter image description here

1 个答案:

答案 0 :(得分:1)

假设您有一个基于tabbar的项目,其中有两个视图控制器FirstVC和SecondVC。 在firstVC中,您有一个textField,并且您希望textFiled在SecondVC中使用。您可以在shouldSelectViewController委托方法中传递textFields。所以这里的FirstVC看起来应该是

class FirstViewController: UIViewController,UITabBarControllerDelegate {

    @IBOutlet weak var txtFldFirst: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tabBarController?.delegate = self;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool
    {
        NSLog("view controller descriptionn should select = %@", viewController.description);
        let selectedVC:SecondViewController = viewController as! SecondViewController
        selectedVC.txtFld = self.txtFldFirst


        return true;
    }

SecondVC看起来应该是这样的

class SecondViewController: UIViewController {

    var txtFld : UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        NSLog("text fld = %@", txtFld.text);
    }

输出将是FirstVC中出现的textField文本。

对于UITableViewController,请按照相同的方式

import UIKit

class FirstTableViewController: UITableViewController,UITabBarControllerDelegate {

    var txtFldFirst:UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()

        txtFldFirst = UITextField(frame: CGRectMake(20, 100, 150, 30));
        txtFldFirst.text = "Hello World"
        self.view.addSubview(txtFldFirst);

        self.tabBarController?.delegate = self;
    }


    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool
    {
        NSLog("view controller descriptionn should select = %@", viewController.description);
        let selectedVC:SecondTableViewController = viewController as! SecondTableViewController
        selectedVC.txtFld = self.txtFldFirst


        return true;
    }

Second tableVC

import UIKit

class SecondTableViewController: UITableViewController {

    var txtFld:UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()


        NSLog("text fld text = %@", self.txtFld.text);
    }

快乐的编码......