将参数传递给由 NSTimer 调用的方法

Posted

技术标签:

【中文标题】将参数传递给由 NSTimer 调用的方法【英文标题】:Passing parameters to the method called by a NSTimer 【发布时间】:2011-04-30 00:07:03 【问题描述】:

如何将参数传递给 NSTimer 调用的方法?我的计时器如下所示:

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES];

并且我希望能够将字符串传递给方法 updateBusLocation。另外,应该在哪里定义方法 updateBusLocation?在我创建计时器的同一个 .m 文件中?

编辑:

其实我还是有问题。我收到错误消息:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“* -[MapKitDisplayViewController updateBusLocation]:无法识别的选择器发送到实例 0x4623600”

这是我的代码:

- (IBAction) showBus 

//do something

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES];
[txtFieldData release];
 


 - (void) updateBusLocation:(NSTimer*)theTimer
 
      NSLog(@"timer method was called");
      NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]];
if(txtFieldData == busNum.text) 
    //do something else
    
    

编辑#2: 没关系,您的示例代码可以正常工作,感谢您的帮助。

【问题讨论】:

我确定很多人都曾在某个时间点想知道的可靠问题。谢谢! 【参考方案1】:

您需要在目标中定义方法。由于您将目标设置为“自我”,那么是的,同一个对象需要实现该方法。但是您可以将目标设置为您想要的任何其他内容。

userInfo 是一个指针,您可以将其设置为您喜欢的任何对象(或集合),并在计时器触发时将其传递给目标选择器。

希望对您有所帮助。

编辑:...简单示例:

设置计时器:

    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
                              target:self 
                              selector:@selector(handleTimer:) 
                              userInfo:@"someString" repeats:NO];

并在同一个类中实现处理程序(假设您将目标设置为“self”):

- (void)handleTimer:(NSTimer*)theTimer 

   NSLog (@"Got the string: %@", (NSString*)[theTimer userInfo]);


【讨论】:

我还是有点迷茫,你介意给我一个调用方法并将参数传递给该方法的计时器示例吗?【参考方案2】:

您可以使用 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:parameterObj1, @"keyOfParameter1"]; 传递您的参数

一个简单的例子:

[NSTimer scheduledTimerWithTimeInterval:3.0
                                 target:self
                               selector:@selector(handleTimer:)
                               userInfo:@@"parameter1": @9
                                repeats:NO];

- (void)handleTimer:(NSTimer *)timer 
    NSInteger parameter1 = [[[timer userInfo] objectForKey:@"parameter1"] integerValue];

【讨论】:

【参考方案3】:

对于 Swift 4.0:

您可以拥有一个带有任何参数的函数,并使用“scheduledTimer”块来执行您需要重复的代码。

func someFunction(param1: Int, param2: String) 

    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true)  timer in
        print(param1)
        print(param2)
    

完成后请小心调用 timer.invalidate(),以防止它连续运行。

【讨论】:

【参考方案4】:

对于 Swift 这样做,

例如你想用 NSTimer

发送 UILabel
override func viewDidLoad() 
    super.viewDidLoad()

    var MyLabel = UILabel()
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("callMethod:"), userInfo: MyLabel, repeats: false)



 func callMethod(timer:NSTimer)

    var MyLabel:UILabel = timer.userInfo as UILabel


【讨论】:

【参考方案5】:

Swift 中使用 字典字面量 NSTimer 调用的方法传递参数 的其他示例: p>

override func viewDidLoad() 
    super.viewDidLoad()

    let dictionary: [String : AnyObject] = ["first element" : "Jordan",
                                            "second element" : Int(23)]

    NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.41),
                                           target: self,
                                           selector: "foo:",
                                           userInfo: dictionary,
                                           repeats: false)


func foo(timer: NSTimer) 
        let dictionary: [String : AnyObject] = timer.userInfo! as! [String : AnyObject]
        let firstElement: String = dictionary["first element"] as! String
        let secondElement: Int = dictionary["second element"] as! Int
        print("\(firstElement) - \(secondElement)")

【讨论】:

【参考方案6】:

不是对这个问题的直接回答,但因为我在搜索时遇到了这个问题,我需要一些不同的东西,它可能会对某人有所帮助。我想在帮助程序类中调用一个函数,我需要传入UIViewController,而不是使用不允许我在其他地方手动调用该函数的用户信息传递它我创建了另一个计时器将调用的函数和从那里调用我感兴趣的函数。有帮助的解决方法。

Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats:true);

func timerFired() 

myFunction(controller: self)


【讨论】:

以上是关于将参数传递给由 NSTimer 调用的方法的主要内容,如果未能解决你的问题,请参考以下文章

如何将参数传递给由 fork() 创建的进程

java中,如何用POST方法将参数传递给第三方网站

当我们将对象作为参数传递给方法时,为啥会调用复制构造函数?

如何将参数传递给配置单元视图,以便视图根据传递的内容进行更改?

将参数传递给 mat-dialog open 方法

Java中的参数传递