嗨,大家好,我想在滿足條件時從FirstViewController中觸發SecondViewController中的方法。所以我必須使用委託這樣做,我的代碼如下:分配的代表,但函數不被調用
FirstViewController:
import UIKit
import CoreBluetooth
import Foundation
class BluetoothViewController: UIViewController, ValueChangedDelegate {
if (characteristic.UUID == CBUUID(string: "2AF0")){
DataReceived.Single_Axis = Double(CharValue)
func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
let singleaxisview = SingeAxisViewController()
singleaxisview.delegate = self
singleaxisview.valueChangedNotifyEveryone()
println("btfired")
}
SecondViewController
import UIKit
import CoreGraphics
protocol ValueChangedDelegate
{
func valueChanged(nValue : String)
}
class SingeAxisViewController: UIViewController {
var delegate : ValueChangedDelegate?
func valueChangedNotifyEveryone()
{
println("hello")
delegate?.valueChanged("changed value")
}
有人能指出我什麼,我錯過了什麼?
你在哪裏初始化'BTController'變量? – t4nhpt
at secondViewController line ViewDidLoad函數 –
這不是初始化,'BTController'仍然爲零。我敢打賭你的代碼會在'BTController!.delegate = self'中崩潰,因爲你正試圖解開一個零變量。 – t4nhpt