加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 使用自定义选项卡栏控制器类在两个视图控制器之间传递数

发布时间:2020-12-14 17:43:08 所属栏目:百科 来源:网络整理
导读:我试图使用Tab Bar Controller将数据从一个viewcontroller传递到另一个viewcontroller.我已经实现了一个自定义Tab Bar Controller类.这是我这个类的代码: class CustomTabBarControllerClass: UITabBarController,UITabBarControllerDelegate { override fu
我试图使用Tab Bar Controller将数据从一个viewcontroller传递到另一个viewcontroller.我已经实现了一个自定义Tab Bar Controller类.这是我这个类的代码:

class CustomTabBarControllerClass: UITabBarController,UITabBarControllerDelegate {

    override func awakeFromNib() {
        self.delegate = self;
    }

    func tabBarController(tabBarController: UITabBarController,didSelectViewController viewController: UIViewController) {
        var logView = SecondViewController()
        logView.log.append("Testing 123")
    }

}

正如您在我的代码中看到的,我正在使用logView变量创建SecondViewController的实例.在我的SecondViewController类中,我有一个日志数组设置,它将保存从CustomTabBarControllerClass类传递的值.这是我的SecondViewController的代码.

class SecondViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var log = [String]()

    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return log!.count
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("logCell",forIndexPath: indexPath) as UITableViewCell

        cell.textLabel.text = log![indexPath.row]

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        println(log!) //fatal error: unexpectedly found nil while unwrapping an Optional value
    }

}

在我的viewDidLoad()函数中,我试图用println(log!)将日志打印到控制台.当此代码运行时,我出现以下错误:致命错误:在展开Optional值时意外发现nil.那么我如何在两个viewcontrollers之间传递数据呢?

更新

didSelectViewController函数已使用下面的代码更新,但是,我仍然收到相同的错误消息.

func tabBarController(tabBarController: UITabBarController,didSelectViewController viewController: UIViewController) {
        var logView = self.viewControllers![0] as SecondViewController
        logView.log?.append("Testing 123")
    }

解决方法

您的标签栏控制器已经有一个SecondViewController实例,因此您不应该实例化一个新实例.使用选项卡栏控制器的viewControllers属性来访问所需的属性(可能来自名称,索引为1的那个).

class ViewController: UIViewController {

    var log = [String]()

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        println(log)
    }
}

在标签栏控制器中,

class RDTabBarController: UITabBarController,UITabBarControllerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func tabBarController(tabBarController: UITabBarController,didSelectViewController viewController: UIViewController) {
        var logView = self.viewControllers![1] as ViewController
        logView.log.append("Testing 123")
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读