我正在創建一個可達性警報,我在Swift中編寫並正在被最初寫入目標C的應用程序中調用。我已正確設置了所有的橋接頭-h。當我點擊一個在Swift中創建的UIButton和一個也是用swift編寫的IBAction時,問題就在發生。下面是錯誤和代碼。有沒有辦法來防止這次崩潰?目標C中Swift UIButton和IBAction崩潰
終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [NSNotificationQueue buttonTapped:]:無法識別的選擇發送到實例0x7ae602d0'
// NetworkErrorView.swift
import Foundation
import UIKit
@objc class NetworkErrorView: NSObject {
var xCoordinate:CGFloat = 0
var yCoordinate: CGFloat = 0
var width: CGFloat = 0
var height: CGFloat = 0
var reTryButton = UIButton()
func renderAlertView() -> UIView {
let alert = UIAlertView()
alert.title = "Connection Failed"
alert.message = "Your device is not connected to the Internet. Please try again later."
alert.addButtonWithTitle("OK")
alert.show()
return alert
}
func renderReTryButton() -> UIView {
var buttonImage = UIImage(named: "[email protected]")
reTryButton.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: UIControlEvents.TouchUpInside)
reTryButton.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
reTryButton.frame = CGRect(x: 18, y: 470, width: 290, height: 30)
println("stopes here")
return reTryButton
}
@IBAction func buttonTapped (sender: AnyObject!) {
let alertView = UIAlertView()
alertView.title = "Connection Failed"
alertView.message = "Your device is not connected to the Internet. Please try again later."
alertView.addButtonWithTitle("OK")
alertView.show()
}
func render() -> UIView {
var networkErrorView = UIView()
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width;
let screenHeight = screenSize.height;
if screenHeight == 480 {
xCoordinate = 0
yCoordinate = 74
width = 320
height = 361
var imgNoConnection = UIImage(named: "Noconnectionz.png")
networkErrorView = UIImageView(image: imgNoConnection)
}
else if screenHeight == 568 {
xCoordinate = 0
yCoordinate = 74
width = 320
height = 443
var imgNoConnection = UIImage(named: "[email protected]")
networkErrorView = UIImageView(image: imgNoConnection)
}
return networkErrorView
}
}
這裏是我稱之爲目標C
Swift *connectivity = [[Swift alloc]init];
bool swiftConnected = [connectivity reachability];
if (!swiftConnected) {
NetworkErrorView *networkErrorView = [[NetworkErrorView alloc]init];
[networkErrorView renderAlertView];
UIView *subView = [networkErrorView render];
[super.view addSubview: subView];
[super.view addSubview:[networkErrorView renderReTryButton]];
}
}
嘗試anyObject而不展開它? @IBAction func buttonTapped(sender:AnyObject) – domshyra
我試過了,我試過讓它成爲UIButton的發件人。 – ArmorKing1212
當我拿出這行代碼:reTryButton.addTarget(self,action:Selector(「buttonTapped:」),forControlEvents:UIControlEvents.TouchUpInside)我可以單擊按鈕而不會崩潰,但我顯然需要分配給按鈕。我想知道其他人是否有這個問題。我正在使用xcode6 beta 3. – ArmorKing1212