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

问题描述 投票:12回答:4

我使用cocoapods安装MBProgressHUB和桥接头我不能这样做

 #import "MBProgressHUD.h"

我换了

#import "MBProgressHUD/MBProgressHUD.h"

导入是可以的,但我不能在swift代码中使用它?我做错了什么?我怎么解决这个问题?

ios swift mbprogresshud
4个回答
26
投票

试试这个:

1)在use_frameworks!中指定Podfileuse 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
投票
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.

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)

-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. SHOW - self.showHUD(progressLabel:“Loading ...”)

2. HIDE - self.dismissHUD(isAnimated:true)

© www.soinside.com 2019 - 2024. All rights reserved.