【问题标题】:MBProgressHUD not working in swift: cannot import and useMBProgressHUD 无法快速工作:无法导入和使用
【发布时间】:2015-08-25 01:41:16
【问题描述】:

我使用 cocoapods 安装 MBProgressHUB 并且在桥接头中我不能这样做

 #import "MBProgressHUD.h"

我改成

#import "MBProgressHUD/MBProgressHUD.h"

导入没问题,但我不能在 swift 代码中使用它?我做错什么了吗?我该如何解决这个问题?

【问题讨论】:

    标签: ios swift mbprogresshud


    【解决方案1】:

    试试这个:

    1) 在Podfileuse frameworks 中指定use_frameworks!(而不是静态库)。

    这是添加以 Swift 编写的 pod 作为依赖项所必需的,如果您的应用程序是用 Swift 编写的,这通常是一个好主意。

    2) 做pod install

    这可确保您的项目设置为实际使用上述内容。

    3) 在您的桥接头中添加#import <MBProgressHUD/MBProgressHUD.h>(注意尖括号-不是引号),并在需要使用它的Swift 类中添加import MBProgressHUD

    也就是说,

    MyApp-Bridging-Header.h :

    #import <MBProgressHUD/MBProgressHUD.h>
    // ... other imports ...
    

    这会将 Objective-C 文件暴露给 Swift。尖括号表示这实际上是在导入框架。

    MyViewController.swift :

    import UIKit
    import MBProgressHUD
    // ... other imports...
    
    class MyViewController: UIViewController {
      // ... yada yada...
    }
    

    这实际上会导入依赖项以供您的视图控制器使用。

    【讨论】:

    • 你能解释一下第三步吗?对于 1,是的,我做到了,但我认为这仅适用于基于 swift 的 pod...
    • 对于 datetools 和 sdwebimage 我没有使用锚,但它们仍然有效
    • 请查看修改后的答案。 :]
    • 是的,修改后的答案有效,您介意向我解释一下吗?
    • 添加了更多评论。干杯。
    【解决方案2】:

    在 Swift 3 中添加 Pod 和桥接文件后可以直接这样使用。

      var hud = MBProgressHUD()
      hud = MBProgressHUD.showAdded(to: navigationController?.view, animated: 
      true)
      // Set the custom view mode to show any view.
      hud.mode = MBProgressHUDModeCustomView
      // Set an image view with a checkmark.
      let gifmanager = SwiftyGifManager(memoryLimit:20)
      let gif = UIImage(gifName: "miniballs1.gif")
      let imageview = UIImageView(gifImage: gif, manager: gifmanager)
      hud.labelText = NSLocalizedString("Loading", comment: "")
      hud.labelColor = UIColor.red
      imageview.frame = CGRect(x: 0 , y: 0, width: 25 , height: 25)
      hud.customView = imageview
      // Looks a bit nicer if we make it square.
      hud.show(true)
    

    【讨论】:

      【解决方案3】:
      You can directly drag MBProgressHUD Folder to your swift project, It will create the Bridging header as named "YourAppName-Bridging-Header.h", it means you can import obj-c classes into your swift project.
      
      
      
      'import UIKit
      import MBProgressHUD
      
      class MyViewController: UIViewController {
        // write your code
      }'
      This actually imports the dependency for use by your view controller.
      

      【讨论】:

        【解决方案4】:

        创建易于使用并贯穿整个应用程序的扩展程序

        extension UIViewController {
        
            func showHUD(progressLabel:String){
                DispatchQueue.main.async{
                    let progressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
                    progressHUD.label.text = progressLabel
                }
            }
        
            func dismissHUD(isAnimated:Bool) {
                DispatchQueue.main.async{
                    MBProgressHUD.hide(for: self.view, animated: isAnimated)
                }
            }
        }
        

        用法:

        1. SHOW - self.showHUD(progressLabel: "Loading...")

        2。隐藏 - self.dismissHUD(isAnimated: true)

        【讨论】:

        • OP 询问如何在swift中正确导入和使用MBProgressHud。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 1970-01-01
        • 2022-12-01
        • 2018-04-13
        • 1970-01-01
        相关资源
        最近更新 更多