【问题标题】:Swift UIButton and IBAction crashing in Objective CSwift UIButton 和 IBAction 在 Objective C 中崩溃
【发布时间】:2014-09-10 04:08:27
【问题描述】:

我正在创建一个我用 Swift 编写的可达性警报,并在最初用目标 C 编写的应用程序中被调用。我的所有 bridged-header-.h 设置正确。当我单击在 Swift 中创建的 UIButton 和同样用 swift 编写的 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: "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

}

}

这是我在 Objective 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]];

    } 
   }

【问题讨论】:

  • 尝试任何对象而不打开它? @IBAction func buttonTapped (sender: AnyObject)
  • 我试过了,我也试过让它成为 UIButton 的发件人。
  • 每当我取出这行代码时:reTryButton.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: UIControlEvents.TouchUpInside) 我可以单击按钮而不会崩溃,但是我显然需要分配给按钮的动作。我想知道其他人是否有这个问题。我正在使用 xcode6 beta 3。

标签: ios objective-c uibutton swift ibaction


【解决方案1】:

这是你的问题:

[networkErrorView renderAlertView];
 UIView *subView = [networkErrorView render];

您的函数renderAlertView 没有副作用,也就是说,它所做的只是创建一个 UIView 并将其返回给您。但是即使它没有副作用,你仍然在调用它而不将它分配给某个东西。

我不太确定 render 做了什么(请向我们展示它的代码!),但我的猜测是它不会返回任何内容,它只是呈现 networkErrorView,但您将其分配为UIView。所以,我想你想要更像这样的东西:

UIView *subView = [networkErrorView renderAlertView];
[subView render];

【讨论】:

  • 我将渲染代码添加到我的问题中。当没有互联网连接时,渲染函数创建一个带有覆盖图像的 UIView 和一个告诉用户没有互联网的 UIAlert,在视图中也呈现的按钮是一个重试按钮,用于再次检查连接。问题出在按钮本身。当我添加代码以运行 IBAction 函数时,应用程序崩溃并给我错误消息。
  • @ArmorKing1212 嗯...我不知道那是什么问题。我只能告诉你[NSNotificationQueue buttonTapped] 意味着NSNotificationQueue 的一个实例正在以某种方式获取buttonTapped 方法,而应该是一个按钮。你能告诉我们完整的崩溃日志吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多