MBProgressHUD无法在swift中工作:无法导入和使用

时间:2015-06-10 01:27:55

标签: ios swift mbprogresshud

我使用cocoapods安装MBProgressHUB并在桥接头我不能只做

 #import "MBProgressHUD.h"

我改为

#import "MBProgressHUD/MBProgressHUD.h"

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

4 个答案:

答案 0 :(得分:26)

试试这个:

1)在use_frameworks!use frameworks(而非静态库)中指定Podfile

这是添加使用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 :(得分:1)

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.

答案 2 :(得分:1)

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

  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 :(得分:-1)

创建易于使用且适用于整个应用程序的扩展程序

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。显示-self.showHUD(progressLabel:“正在加载...”)

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