简体   繁体   English

在来自不同控制器的函数中使用两个变量

[英]using two variables in a function from different controllers

I have a function in which I want to use a variable from a different view controller and a variable defined within the function. 我有一个函数,我要使用其他视图控制器中的变量和函数中定义的变量。 The variable from the different view controller is being sent to the second view controller with the following code: 来自不同视图控制器的变量将通过以下代码发送到第二个视图控制器:

func clock(){

    time++

NSNotificationCenter.defaultCenter().postNotificationName("timerValID", object: time)

}


@IBAction func TimerGo(sender: AnyObject) {


    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("clock"), userInfo: nil, repeats: true)


}

The code from the second view controller (function containing two variables) is: 来自第二个视图控制器的代码(包含两个变量的函数)为:

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("location:"), name: "timerValID", object: self.view.window)}


func location(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)(notification: NSNotification){

    var startlocation: CLLocation!
    var lastlocation: CLLocation!
    var traveleddistance: Double = 0
    var speed:CLLocationSpeed = CLLocationSpeed()
    let time = notification.object as! NSNumber

} }

When I run the app, I get the message: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: unrecognized selector sent to instance..... 当我运行应用程序时,我收到消息:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:无法识别的选择器发送到实例.....

The important part of the error is the part you missed and replaced with ".....". 错误的重要部分是您错过并替换为“ .....”的部分。 It would tell you which selector was the problem. 它会告诉您哪个选择器是问题所在。

Nevertheless, looking at your code, there are a couple of problems. 但是,查看您的代码仍然有两个问题。

Firstly, with Swift, you don't use Selector("myFunc") , you just use "myFunc". 首先,在Swift中,您无需使用Selector("myFunc") ,而只需使用“ myFunc”。 Also, the "object:" parameter is the source of the notification. 同样,“ object:”参数是通知的来源。 In most cases you will use "nil" for "any source of this message". 在大多数情况下,“此消息的任何来源”都将使用“ nil”。 So therefore you would have 因此,您将拥有

     override func viewDidLoad() {
         NSNotificationCenter.defaultCenter().addObserver(self, 
             selector: "location:", name: "timerValID", object: nil)
     }

Secondly, your addObserver function will be passed just one object, which is the notification. 其次,您的addObserver函数将仅传递一个对象,即通知。 Therefore, the signature for "location:" should be 因此,“位置:”的签名应为

    func location(notification: NSNotification){     
        //do stuff
    }

This is probably the cause of the error you are seeing, because you don't have a method with the expected signature. 这可能是您看到错误的原因,因为您没有带有预期签名的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为两个不同的视图控制器和数组使用框架 - using a framework for two different view controllers and array 如何从UIViewController的扩展中为两个不同的控制器返回两个不同的表示控制器? - How to return two different presentation controllers for two different controllers from extension of UIViewController? 如何同时从两个不同的控制器管理两个AFNetworking操作 - how to manage concurrently two AFNetworking operations from two different controllers 从不同的视图控制器访问textField.text变量 - Access textField.text variables from different view controllers 从两个不同的视图控制器更新表视图单元 - Update a Table View Cell from two different view controllers 从单个UITabBarItem有条件地产生两个不同的视图控制器 - Conditionally Produce two different view controllers from single UITabBarItem iOS:使用corebluetooth库从不同的视图控制器进行通信 - iOS:Communicate from different view controllers using corebluetooth library 不同视图控制器中的同名变量 - Variables of same name in different view controllers 如何将didSelectViewController与两个不同的控制器一起使用? - How to use didSelectViewController with two different controllers? 在 iOS 中展开到两个不同的视图控制器 - unwinding to two different view controllers in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM