2017-07-16 76 views
0

我試圖將大部分代碼移動到另一個文件;我相信我已經做到了,但是,我在調用函數時遇到了問題。UIBarButtonItem在調用時未在另一個類中調用函數

我有兩個文件:

scannerBrain.swift

var parentView = UIViewController() 

func startScan(){ 
//The code I want to execute is here 
} 

scannerViewController.swift

let scanner = scannerBrain() 
scanner.parentView = self 
let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: scanner, action: #selector(scanner.startScan)) 
navigationItem.rightBarButtonItem = rightButton 

編譯沒有問題,除了startScan不會被調用。我試圖改變rightButton的目標self,但造成的unrecognised selector sent to instance

崩潰感謝所有幫助

回答

1

scanner應該是一流水平的財產,否則將被釋放。像這樣的東西。

class TestViewController: UIViewController { 

    let scanner = scannerBrain() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     scanner.parentView = self 
     let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: scanner, action: #selector(scanner.startScan)) 
     navigationItem.rightBarButtonItem = rightButton 
    } 
} 
+0

謝謝了!你幫了很多忙 – Will