ios - 如何从不同的文件调用此覆盖函数

标签 ios swift

我正在尝试从另一个文件调用函数。另一个文件有我需要的功能,而swift不支持多重继承。

我试图合并这个文件-

import XLPagerTabStrip
//Delete UIViewController, and Extend //ButtonBarPagerTabStripViewController instead
class ParentViewController: ButtonBarPagerTabStripViewController {
    let purpleInspireColor = UIColor(red:0.13, green:0.03, blue:0.25, alpha:1.0)
    override func viewDidLoad() {
          super.viewDidLoad()

    // change selected bar color
    settings.style.buttonBarBackgroundColor = .white
    settings.style.buttonBarItemBackgroundColor = .white
    settings.style.selectedBarBackgroundColor = purpleInspireColor
    settings.style.buttonBarItemFont = .boldSystemFont(ofSize: 14)
    settings.style.selectedBarHeight = 2.0
    settings.style.buttonBarMinimumLineSpacing = 0
    settings.style.buttonBarItemTitleColor = .black
    settings.style.buttonBarItemsShouldFillAvailiableWidth = true
    settings.style.buttonBarLeftContentInset = 0
    settings.style.buttonBarRightContentInset = 0
    changeCurrentIndexProgressive = { [weak self] (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
    guard changeCurrentIndex == true else { return }
    oldCell?.label.textColor = .black
    newCell?.label.textColor = self?.purpleInspireColor
    }
    }

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Phone")
    let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Email")
    return [child_1, child_2]
    }
}

使用此文件 -


import Foundation
import UIKit

class SignupViewController : UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func loginTapped(_ sender: Any) {
           let loginViewController = storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
           present(loginViewController,animated: true, completion: nil)
       }
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Phone")
    let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Email")
    return [child_1, child_2]
    }
}

但是,它们继承自两个不同的基类,因此还没有任何效果。

我想将这两个类放在单独的文件中,并从我需要使用的文件中的类中调用函数,但是当我调用这个特定函数时,我遇到了参数问题。

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    let child_1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Phone")
    let child_2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Email")
    return [child_1, child_2]
    }

最佳答案

您可以使用通用代码创建一些全局函数(在任何类之外),据我所知,您不需要任何参数

func instantiatePhoneAndMailViewControllers() -> [UIViewController] {
    return [UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Phone"), 
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Email")]
}

并从viewControllers(for:)等内部调用它。例如

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    return instantiatePhoneAndMailViewControllers()
}

关于ios - 如何从不同的文件调用此覆盖函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393471/

相关文章:

iOS 9 Swift 2 : horizontally scrollable views? Facebook 的 "suggested friends/groups"

ios - topLayoutGuide 在 vi​​ewWillAppear 之后应用

ios - 在运行时在表格 View 单元格中添加 subview 并展开表格 View 单元格的最佳方法

ios - Swift 中的类型转换

ios - 如何使用 "getControlPoint(at idx: Int, values ptr: UnsafeMutablePointer<Float>!)"CAMediaTimingFunction

iOS 共享扩展在 Chrome 中不起作用

ios - 当 applicationWillEnterForeground 发生时验证剪贴板(快速)

ios - 从另一个 View Controller 访问变量

ios - 为 ios 应用程序生成详细崩溃报告的好库吗?

ios - 如何在iOS Chart Graph Framework中设置多种背景颜色