我有一個標籤,我想在觸摸重新編碼的一段時間內用多個顯示器更新文本。我可以使用performSelector但似乎笨重......如何使用NSTimer更新標籤?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self changeTextForLabel:@"A text (1)"]; // When touch begins the display is changed
[self performSelector:@selector(changeTextForLabel:) withObject:@"Another text (2)" afterDelay:1]; // After 1 second update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And another text (3)" afterDelay:2]; // after 2 seconds update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And even another text (4)" afterDelay:3]; // After 3 seconds update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And yes even another text (5)" afterDelay:3]; }
我聽到人們談論使用定時器來執行的方法,每x秒,但我不知道如何使用它爲我的處境。我有什麼是..
- (void)updateLabel:(NSTimer *)theTimer {
[self changeTextForLabel:@"A text (1)"];
[self changeTextForLabel:@"Another text (2)"];
[self changeTextForLabel:@"And another text (3)"];
[self changeTextForLabel:@"And even another text (4)"];
[self changeTextForLabel:@"And yes even another text (5)"];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES]; }
但是,這隻顯示最後的消息..但我希望它在1秒後一個接一個地顯示。 我試圖在每條消息之間使用pause()或sleep(),但它只會延遲標籤更新之前的時間,並會使用最後一條消息進行更新。
在 - (NSArray)標題方法,如果我使用self.titles它崩潰。你知道爲什麼嗎? 另外,當顯示完成時,我怎樣才能讓它再次通過顯示器,當我點擊? –
Inside - (NSArray *)標題,如果您使用self.titles你崩潰,因爲self.titles調用 - (NSArray *)標題;點符號只是調用getter/setter的快捷方式。所以你會因爲你遞歸調用getter而崩潰,遞歸永遠不會結束。使用_titles而不是self.titles。 –
要再次顯示5個標題,您需要重置titles數組。您只需將標題設置爲零即可完成此操作,以便當您再次調用self.titles時,該數組將被重新初始化。我編輯了答案,使其更清晰。 –