已分配的委托,但未调用函数

时间:2015-10-15 08:44:40

标签: ios swift delegates

大家好我想在条件满足时从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")
}

有人能指出我错过了什么吗?

1 个答案:

答案 0 :(得分: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.
    }
    */

}