【问题标题】:Reachability error when switching from Wifi to Cellular in Swift在 Swift 中从 Wifi 切换到 Cellular 时出现可达性错误
【发布时间】:2015-12-04 22:13:57
【问题描述】:

您好,我遇到一个问题,当我将连接从 wifi 更改为蜂窝网络或反之亦然时,我的应用程序崩溃,而不是像我编程的那样显示“无连接”视图控制器。我收到这样的错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Spotter.AppDelegate checkReachability]:无法识别的选择器发送到实例 0x17404b1f0”

我正在使用 Apple 提供的 Objective-C 示例,顺便将它与我的 Swift 代码桥接。

这是我的代码:

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

var window: UIWindow?
var reachability : Reachability?



    NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability", name: kReachabilityChangedNotification, object: nil);

    self.reachability = Reachability.reachabilityForInternetConnection();

    self.reachability!.startNotifier();

return true

}

func checkReachability(notification:NSNotification)
{

    var remoteHostStatus = self.reachability!.currentReachabilityStatus()

    if (remoteHostStatus.value == NotReachable.value)

    {

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let offlineview = mainStoryboard.instantiateViewControllerWithIdentifier("OfflineViewController") as! OfflineViewController
        window!.rootViewController = offlineview
        window!.makeKeyAndVisible()



    }else{

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabBar") as! UITabBarController

        window!.rootViewController = initialViewController
        window!.makeKeyAndVisible()


    }
}

谁能告诉我如何解决这个问题? 谢谢

【问题讨论】:

    标签: ios objective-c swift reachability unrecognized-selector


    【解决方案1】:

    您的选择器名称中缺少冒号。应该是:

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkReachability:", name: kReachabilityChangedNotification, object: nil);
    

    【讨论】:

    • 哇,谢谢!我不敢相信我错过了。你知道吗,我可以显示没有连接而不必重新加载视图?
    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 2016-09-25
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多