gpt4 book ai didi

ios - 如何创建可从 View Controller 访问的函数?

转载 作者:行者123 更新时间:2023-11-30 13:13:18 24 4
gpt4 key购买 nike

我想定义一种方法来显示和隐藏应用程序中的进度条。但是,我不想在每个 View Controller 中为这些创建本地方法。如何定义将 View 作为参数并对其执行某些操作的全局方法。

这是我尝试过的:

import Foundation
import MBProgressHUD

public class ShowLoading{

let view = UIView()

func show(){
let loadingNotification = MBProgressHUD.showHUDAddedTo(view, animated: true)
loadingNotification.color = UIColor.blueColor()
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = MyConstants.LOADING_TEXT
}

func hide(){
MBProgressHUD.hideAllHUDsForView(view, animated: true)
}

}

最佳答案

让一个BaseViewController类继承UIViewController

现在在 BaseViewController 中创建方法

func show() {
let loadingNotification = MBProgressHUD.showHUDAddedTo(view, animated: true)
loadingNotification.color = UIColor.blueColor()
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = MyConstants.LOADING_TEXT
}

func hide(){
MBProgressHUD.hideAllHUDsForView(view, animated: true)
}

现在继承这个BaseViewController给你想要显示进度条的ViewController,然后调用self.show()self.hide()

关于ios - 如何创建可从 View Controller 访问的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499973/

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