我正在尝试将 objective-C/CocoaPods 代码 ( GBFlatButton ) 添加到现有的 swift 代码中。我阅读了所有关于页眉设置的信息,我这样做了,让我知道是否表现良好。
头文件中还添加了正确的导入文件。
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <UIKit/UIKit.h>
#import <GBFlatButton/GBFlatButton.h>
#import <GBFlatButton/UIColor+GBFlatButton.h>
另一方面,当我编译代码时,CocoaPods 项目没有给我一个错误。当我使用按钮时发生错误,可能是我尝试使用此 objective-c 代码的方式:
let MyButton: GBFlatButton! = GBFlatButton(frame: CGRect(x: 0.0, y: 0.0, width: 200.0, height: 40.0))
我也试过这样使用它:@IBOutlet var FButton: GBFlatButton!
这是我遇到的最后一个错误:
/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:19: Use of undeclared type 'GBFlatButton'
/Users//Documents/Projects/party/party/party/partyStepViewController.swift:18:9: Could not infer type for 'MyButton'
任何线索都会有所帮助:)
谢谢!!如果您需要更多信息,请随时问我。
最佳答案
您忘记在应用程序的build设置中添加桥接头
在 View Controller 中:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button: GBFlatButton! = GBFlatButton(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
只要这样做就可以了
关于iOS - 在 Swift 代码中使用 Cocoapods objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738647/