gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:23:43 30 4
gpt4 key购买 nike

我正在尝试从另一个文件调用函数。另一个文件有我需要的功能,而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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com