2010-10-25 27 views
55

Làm cách nào để chuyển một tham số đến phương thức được gọi bởi NSTimer? Bộ hẹn giờ của tôi trông giống như sau:Truyền tham số cho phương thức được gọi bởi một NSTimer

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

và tôi muốn có thể chuyển chuỗi vào phương thức updateBusLocation. Ngoài ra, ở đâu phải định nghĩa phương thức updateBusLocation? Trong cùng một tập tin .m mà tôi tạo ra bộ đếm thời gian?

EDIT:

Thật sự tôi vẫn đang gặp vấn đề. Tôi nhận được thông báo lỗi:

Chấm dứt ứng dụng do ngoại lệ còn tự do 'NSInvalidArgumentException', lý do: '* - [MapKitDisplayViewController updateBusLocation]: chọn không được công nhận gửi đến dụ 0x4623600'

Đây là mã của tôi:

- (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 
    } 
    } 

EDIT # 2: Đừng bận tâm mã ví dụ của bạn hoạt động tốt nhờ trợ giúp.

+0

Câu hỏi rắn chắc chắn có rất nhiều người đã tự hỏi tại điểm này hay điểm khác. Cảm ơn! –

Trả lời

95

Bạn cần phải xác định phương pháp trong mục tiêu. Vì bạn đặt mục tiêu là 'self', nên có cùng một đối tượng đó cần thực hiện phương thức. Nhưng bạn có thể đặt mục tiêu vào bất kỳ mục tiêu nào khác mà bạn muốn.

userInfo là một con trỏ mà bạn có thể đặt cho bất kỳ đối tượng nào (hoặc bộ sưu tập) bạn thích và sẽ được chuyển đến bộ chọn mục tiêu khi bộ hẹn giờ kích hoạt.

Hy vọng điều đó sẽ hữu ích.

EDIT: ...Ví dụ đơn giản:

Thiết lập bộ đếm thời gian:

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

và thực hiện xử lý trong cùng một lớp (giả sử bạn đang thiết lập các mục tiêu để 'tự'):

- (void)handleTimer:(NSTimer*)theTimer { 

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

} 
+0

Tôi vẫn cảm thấy lạc lõng khi bạn cho tôi một ví dụ về bộ hẹn giờ gọi một phương thức và chuyển một tham số cho phương thức đó? – bubster

+0

Chắc chắn, tôi đã chỉnh sửa câu trả lời của mình bằng một ví dụ đơn giản –

22

Bạn có thể chuyển đối số của bạn với userInfo: [NSDictionary dictionaryWithObjectsAndKeys:parameterObj1, @"keyOfParameter1"];

Ví dụ đơn giản:

[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]; 
} 
1

Đối Swift làm như thế này,

Ví dụ bạn muốn gửi UILabel với NSTimer

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 

} 
2

dụ bổ sung trong Swift sử dụng điển đen cho chuyển các tham số đến phương thức được gọi bởi NSTimer:

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)") 
} 
Các vấn đề liên quan