2015-10-15 20 views
0

嗨,大家好,我想在滿足條件時從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") 
} 

有人能指出我什麼,我錯過了什麼?

+0

你在哪裏初始化'BTController'變量? – t4nhpt

+0

at secondViewController line ViewDidLoad函數 –

+0

這不是初始化,'BTController'仍然爲零。我敢打賭你的代碼會在'BTController!.delegate = self'中崩潰,因爲你正試圖解開一個零變量。 – t4nhpt

回答

1

而不是BTController!.delegate = self你將不得不使用FirstViewController的對象並設置委託,你還需要初始化FirstViewController對象。這裏是一個示例代碼,如果你想實現它。第一視圖控制器實現委託方法和btnClicked的呼叫的第二個視圖控制器被調用並且你獲得響應回第一視圖控制器在的valueChanged方法

它實現了協議和也
import UIKit 

class FirstViewController: UIViewController, ValueChangedDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     lbl.text = "inital value" 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 
    } 
    */ 

    @IBOutlet weak var lbl: UILabel! 


    @IBAction func btnClicked(sender: AnyObject) 
    { 
     let secondViewController = SecondViewController() 
     secondViewController.delegate = self 
     secondViewController.valueChangedNotifyEveryone() 
    } 



    func valueChanged(nValue : String) 
    { 
     print(nValue) 
     lbl.text = nValue 
    } 


} 

第二視圖控制器從valueChangedNotifyEveryone方法調用協議方法

import UIKit 


protocol ValueChangedDelegate 
{ 
    func valueChanged(nValue : String) 
} 


class SecondViewController: UIViewController { 


    var delegate : ValueChangedDelegate? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    func valueChangedNotifyEveryone() 
    { 

     delegate?.valueChanged("changed value") 
    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+0

嗨,感謝您的幫助。不幸的是我會得到錯誤的代碼。 1. FirstViewController不符合協議ValueChanged 2. singleaxisview.delegate = self不能將類型'FirstViewController的值分配給類型「ValueChanged」 –

+0

您得到了什麼錯誤? –

+0

1. FirstViewController不符合協議ValueChanged 2. singleaxisview.delegate = self無法將類型'FirstViewController的值分配給類型「ValueChanged」 –