2014-07-18 55 views
0

我正在創建一個可達性警報,我在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]]; 

    } 
    } 
+0

嘗試anyObject而不展開它? @IBAction func buttonTapped(sender:AnyObject) – domshyra

+0

我試過了,我試過讓它成爲UIButton的發件人。 – ArmorKing1212

+0

當我拿出這行代碼:reTryButton.addTarget(self,action:Selector(「buttonTapped:」),forControlEvents:UIControlEvents.TouchUpInside)我可以單擊按鈕而不會崩潰,但我顯然需要分配給按鈕。我想知道其他人是否有這個問題。我正在使用xcode6 beta 3. – ArmorKing1212

回答

0

這是你的問題:

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

您的功能renderAlertView沒有任何副作用,也就是說,它只是創建一個UIView並將其返還給您。但即使它沒有副作用,你仍然在調用它而沒有將它分配給某些東西。

我不太知道什麼render呢(請告訴我們的代碼!),但我的猜測是,它不返回任何東西,它只是呈現networkErrorView,但你作爲一個UIView分配它。所以,我想你想要更類似這樣的東西:

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

我將渲染代碼添加到了我的問題中。當沒有互聯網連接時,渲染函數會創建一個帶覆蓋圖像的UIView和一個UIAlert,告訴用戶沒有互聯網,同樣在視圖中呈現的按鈕是一個重試按鈕,用於再次檢查連接。問題在於它是自己的按鈕。當我添加代碼來運行IBAction函數時,應用程序崩潰並給出錯誤消息。 – ArmorKing1212

+0

@ ArmorKing1212嗯......我不知道那是什麼問題。我可以告訴你的是'[NSNotificationQueue buttonTapped]'意味着'NSNotificationQueue'的一個實例以某種方式得到'buttonTapped'方法,而應該是一個按鈕。你能告訴我們完整的崩潰日誌嗎? –