Ios Swift UIButton和IBAction在目标C中崩溃
我正在创建一个我在Swift中编写的可达性警报,并在最初在objective C中编写的应用程序中调用。我的所有桥接头-.h设置都正确。当我点击Swift中创建的UIButton和同样用Swift编写的iAction时,问题就出现了。下面是错误和代码。有没有办法防止这次撞车 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSNotificationQueue buttonTapped:::]:未识别的选择器发送到实例0x7ae602d0'Ios Swift UIButton和IBAction在目标C中崩溃,ios,objective-c,uibutton,swift,ibaction,Ios,Objective C,Uibutton,Swift,Ibaction,我正在创建一个我在Swift中编写的可达性警报,并在最初在objective C中编写的应用程序中调用。我的所有桥接头-.h设置都正确。当我点击Swift中创建的UIButton和同样用Swift编写的iAction时,问题就出现了。下面是错误和代码。有没有办法防止这次撞车 由于未捕获的异常“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: "Btn-Try-Again@2x.png")
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: "Noconnection@2x.png")
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]];
}
}
这是你的问题:
[networkErrorView renderAlertView];
UIView *subView = [networkErrorView render];
您的函数renderAlertView
没有任何副作用,也就是说,它所做的只是创建一个UIView并将其返回给您。但是,即使它没有副作用,您仍然在调用它,而没有将其指定给某些对象
我不太清楚render
是做什么的(请向我们展示代码!),但我猜它不会返回任何东西,它只是呈现networkErrorView
,但您将其指定为UIView
。所以,我想你想要更像这样的东西:
UIView *subView = [networkErrorView renderAlertView];
[subView render];
尝试任何对象而不展开它@iAction func buttonTapped(发件人:AnyObject)我已经尝试过了,我也尝试过让它成为UIButton的发件人。当我取出这行代码时:reTryButton.addTarget(self,action:Selector(“buttonTapped:”),forControlEvents:UIControlEvents.TouchUpInside),我可以单击按钮而不会崩溃,但我显然需要分配给按钮的操作。我想知道是否有其他人有这个问题。我使用的是xcode6 beta 3。我在问题中添加了渲染代码。当没有internet连接时,render函数将创建一个UIView,其中包含一个覆盖图像和一个UIAlert,告知用户没有internet,视图中也呈现的按钮是一个重试按钮,用于再次检查连接。问题在于按钮本身。当我添加代码以运行IBAction函数时,应用程序崩溃并给出错误消息。@ArmorKing1212 Hmmm。。。我不知道那是什么问题。我所能告诉您的是,
[NSNotificationQueue buttonTapped]
意味着NSNotificationQueue
的一个实例以某种方式获得了方法buttonTapped
,它应该是一个按钮。你能给我们看完整的事故记录吗?