我正在开发一个使用swift通过蓝牙与设备通信的iPhone应用程序。首先,我有一个bluetoothViewController()接收数据和SingleAxisViewController()来显示和动画收到的数据。以下是我目前的代码
BluetoothViewController
import UIKit
import CoreBluetooth
import Foundation
class BluetoothViewController: UIViewController, CBCentralManagerDelegate,CBPeripheralDelegate,UITableViewDelegate,UITableViewDataSource,ValueChangedDelegate {
struct DataReceived {
static var Parameter_1: Double = 90
}
.
.
.
func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
var hashedData = characteristic.value
//Conversion hashedData into Double type
DataReceived.Parameter_1 = Double(hashedData)
let singleaxisview = SingeAxisViewController()
singleaxisview.delegate = self
singleaxisview.valueChangedNotifyEveryone()
}
func valueChanged(nValue : String)
{
println("do nothing")
print(nValue)
}
Parameter1ViewController
import UIKit
import CoreGraphics
protocol ValueChangedDelegate
{
func valueChanged(nValue : String)
}
class SingeAxisViewController: UIViewController {
var delegate : ValueChangedDelegate?
@IBOutlet weak var Needle: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
setAnchorPoint()
var image: UIImage = UIImage(named: "arrow")!
Needle = UIImageView(image: image)
}
func valueChangedNotifyEveryone()
{
angle = BluetoothViewController.DataReceived.Parameter_1
var image: UIImage = UIImage(named: "arrow")!
Needle = UIImageView(image: image)
Needle.transform = CGAffineTransformMakeRotation((CGFloat(angle*14.2) * CGFloat(M_PI) / 180.0))
}
func setAnchorPoint() {
angle = BluetoothViewController.DataReceived.Parameter_1
Needle.transform = CGAffineTransformMakeRotation((CGFloat(angle*14.2) * CGFloat(M_PI) / 180.0))
}
所以我的问题是当我进入Parameter1ViewController
时,图像可以旋转并起作用。但是当蓝牙值改变时,函数valueChangedNotifyEveryone()
被触发并发生错误:
在展开“可选”值时发现nil
在
Needle.transform = CGAffineTransformMakeRotation((CGFloat(angle*14.2) * CGFloat(M_PI) / 180.0))
有人能说明原因吗?