来自动态创建的按钮的 Swift 3 IOS 访问类方法
Posted
技术标签:
【中文标题】来自动态创建的按钮的 Swift 3 IOS 访问类方法【英文标题】:Swift 3 IOS Access Class Method from dynamically created button 【发布时间】:2017-05-01 13:16:50 【问题描述】:我正在尝试从代码生成的按钮触发方法...
我收到错误:类型值:SBSbarcode 选择器没有成员 StartScanning。
此按钮如何访问上述方法?
任何帮助将不胜感激。
class scanViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
setupManualScanner()
func setupManualScanner()
// Scandit Barcode Scanner Integration
// The following method calls illustrate how the Scandit Barcode Scanner can be integrated
// into your app.
// Create the scan settings and enabling some symbologies
let settings = SBSScanSettings.default()
let symbologies: Set<SBSSymbology> = [.code39, .code128]
settings.settings(for: .code128).activeSymbolCounts = [11,15,16,17, 18]
settings.settings(for: .code39).activeSymbolCounts = [10, 14]
for symbology in symbologies
settings.setSymbology(symbology, enabled: true)
let guiOptions = SBSGuiStyle.laser
// Create the barcode picker with the settings just created
let barcodePicker = SBSBarcodePicker(settings:settings)
// Add the barcode picker as a child view controller
addChildViewController(barcodePicker)
view.addSubview(barcodePicker.view)
barcodePicker.didMove(toParentViewController: self)
// Set the allowed interface orientations. The value UIInterfaceOrientationMaskAll is the
// default and is only shown here for completeness.
barcodePicker.allowedInterfaceOrientations = .portrait
// Set custom options
barcodePicker.overlayController.setVibrateEnabled(false)
barcodePicker.overlayController.guiStyle=guiOptions
let btn: UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
btn.backgroundColor = UIColor.green
btn.setTitle("Click to scan", for: .normal)
btn.layer.cornerRadius = 10
btn.layer.masksToBounds = true
btn.addTarget(self, action: #selector(startManualScan), for: .touchUpInside)
barcodePicker.view.addSubview(btn)
// Set the delegate to receive scan event callbacks
barcodePicker.scanDelegate = self
barcodePicker.startScanning(inPausedState: true)
func startManualScan()
self.barcodePicker.startScanning()
【问题讨论】:
是 StartScanning 还是 startScanning ? startScanning,在上面的方法中效果很好但是在按钮动作中无法触发 self.barcodePicker 在哪里声明? 在上述方法中...但即使将按钮操作更改为:barcodePicker.startScanning() 也不起作用 问题是,我猜如何从 startManualScan 方法访问 setupManualScanner 方法的对象 【参考方案1】:试试下面的代码:
类 scanViewController: UIViewController
let barcodePicker = SBSBarcodePicker // declare here
override func viewDidLoad()
super.viewDidLoad()
setupManualScanner()
func setupManualScanner()
// Scandit Barcode Scanner Integration
// The following method calls illustrate how the Scandit Barcode Scanner can be integrated
// into your app.
// Create the scan settings and enabling some symbologies
let settings = SBSScanSettings.default()
let symbologies: Set<SBSSymbology> = [.code39, .code128]
settings.settings(for: .code128).activeSymbolCounts = [11,15,16,17, 18]
settings.settings(for: .code39).activeSymbolCounts = [10, 14]
for symbology in symbologies
settings.setSymbology(symbology, enabled: true)
let guiOptions = SBSGuiStyle.laser
// Create the barcode picker with the settings just created
barcodePicker = SBSBarcodePicker(settings:settings) // made change here
//continue your code
func startManualScan()
barcodePicker.startScanning()
【讨论】:
成功了!谢谢。加上 () --> 让barcodePicker = SBSBarcodePicker() // 在这里声明 欢迎 ..@Bubu以上是关于来自动态创建的按钮的 Swift 3 IOS 访问类方法的主要内容,如果未能解决你的问题,请参考以下文章
创建一个viewcontroller并将现有VC的标签和按钮添加到新的VC Swift iOS
创建视图控制器并将现有 VC 的标签和按钮添加到新的 VC Swift iOS