0

NSTimer で pokeme:@"1" または pokeme:1 または pokeme:"sdfsdfs" を使用できません。エラーが表示されます。これをどのように修正しますか?

- (void)Anything {

    [NSTimer scheduledTimerWithTimeInterval:06.00f target:self selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO];
}

- (void)Pokeme:(NSString *)text {

}
4

4 に答える 4

1

それはできません — セレクターは単なるメソッドの名前であり、そのメソッドへの引数はタイマーになります。必要な動作を含む新しいメソッドを作成してセレクターとして渡すNSTimer+Blocksなどを使用する必要があります。

于 2012-05-02T23:58:46.060 に答える
1

セレクターを正しく呼び出していないか、-Pokeme: で正しい引数を取っていません。

  1. @selector(Pokeme:) でセレクターを呼び出す必要があります
  2. -PokeMe: タイマーを引数として取る必要があります ( NSTimer のドキュメントをもう一度読むことをお勧めします)。
  3. -Pokeme で必要なデータを渡すには、必ず userInfo を使用してください。
于 2012-05-02T23:59:04.127 に答える
0

これは有効なセレクタではありません。セレクターはメソッド シグネチャメソッドの名前にすぎません。パラメーターを渡すことはできないため、パラメーターを指定する必要があります。

[NSTimer scheduledTimerWithTimeInterval:6.00f target:self selector:@selector(pokeMe:) userInfo:nil repeats:NO];

NSTimer docsによると、受信メソッドの署名はフォームから外れている必要があります

- (void)timerFireMethod:(NSTimer *)theTimer

したがって、メソッドを次のように定義する必要があります

- (void)pokeMe:(NSTimer *)timer;

引数に沿って追加情報を渡したい場合userInfoは、型を受け入れ、オブジェクトidから取得できます。timer

実際の例は次のとおりです

[NSTimer scheduledTimerWithTimeInterval:06.00f target:self selector:@selector(pokeMe:) userInfo:@"I was passed" repeats:NO];

それから

- (void)pokeMe:(NSTimer *)timer;
{
    NSLog(@"%@", timer.userInfo);
}

#=> 2012-05-03 00:57:40.496 Example[3964:f803] I was passed
于 2012-05-03T00:02:27.933 に答える
0

を参照してくださいNSTimer Class Reference

これは変数を渡す正しい方法ではなく、実際にはコンパイラがスローされます。オブジェクトを渡すために使用userInfoすると、そのオブジェクトをNSString必要なものに変換できます。

/* INCORRECT */
[NSTimer scheduledTimerWithTimeInterval:06.00f target:self 
                        selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO];

基本的にuserInfo、オブジェクトにすることができます:

NSString

/* Pass @"1" as the userInfo object. */
[NSTimer scheduledTimerWithTimeInterval:6 target:self 
                           selector:@selector(Pokeme:) userInfo:@"1" repeats:NO];

/* Convert the object to NSString. */
-(void)Pokeme:(NSTimer*)timer {
    NSString *passedString = (NSString*)timer.userInfo;
}

NSDictionary

/* Create an NSDictionary with 2 Key/Value objects. */
NSDictionary *passTheseKeys = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"Some String Value 1", @"StringKey1", 
                                   @"Some String Value 2", @"StringKey2", nil];

/* Pass the NSDictionary we created above. */
[NSTimer scheduledTimerWithTimeInterval:6 target:self 
                  selector:@selector(Pokeme:) userInfo:passTheseKeys repeats:NO];

/* Convert the timer object to NSDictionary and handle it in the usual way. */
- (void)Pokeme:(NSTimer*)timer {
    NSDictionary *passed = (NSDictionary *)[timer userInfo];
    NSString * objectString1 = [passed objectForKey:@"StringKey1"];
    NSString * objectString2 = [passed objectForKey:@"StringKey2"];
}
于 2012-05-03T00:05:28.940 に答える