ArmorKing1212
ArmorKing1212

Reputation: 21

Swift UIButton and IBAction crashing in Objective C

I am creating a reachability alert that I wrote in Swift and is being called in the app that is written originally in objective C. I have all my bridged-header-.h setup correctly. The problem is happening when I click on a UIButton created in Swift and an IBAction that is also written in swift. below is the error and code. Is there a way to prevent this crash?

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNotificationQueue buttonTapped:]: unrecognized selector sent to instance 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

}

}

here is where I am calling it in 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]];

    } 
   }

Upvotes: 0

Views: 1701

Answers (1)

Eliza Wilson
Eliza Wilson

Reputation: 1101

Here is your problem:

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

Your function renderAlertView has no side effects, that is, all it does is create a UIView and give it back to you. But even though it has no side effects, you are still calling it without assigning it to something.

I'm not quite sure what render does (please show us the code for that!), but my guess is that it doesn't return anything, it just renders networkErrorView, but you are assigning it as a UIView. So, I'm thinking you want something more like this:

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

Upvotes: 0

Related Questions