如何使用每 N 秒更改 NSString 的值?
我有这段代码每五秒更改 NSString
中的数字。
我将如何保持数字循环运行?现在它从 1 运行到 19 ,并在最后一个 (19) 处停止,并在行上显示 SIGABRT: label.text = ...
如何从显示的第一个数字 (0) 开始,在第一个计时器触发之前?
这是代码:
-(IBAction) rotate3
{
NSString *number = [self.dayArray description];
NSArray *array = [[NSArray alloc] initWithObjects: @"0", @"1", @"2",..., @"19",nil];
number = @"0" ;
numberCount++ ;
self.dayArray = array;
[array release];
label.text = [NSString stringWithFormat:@"Day: %@ ", [dayArray objectAtIndex :numberCount ]];
}
//and the timer
- (void)viewDidLoad
{
timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:YES];
}
I have this code to change the number in the NSString
every five seconds.
How will I keep the numbers running in a loop? It now runs from 1 to 19 ,and stops at the last one (19) with a SIGABRT on the line: label.text = ...
How can I start with the first number displayed (0), before the first timer fires?
Here is the code:
-(IBAction) rotate3
{
NSString *number = [self.dayArray description];
NSArray *array = [[NSArray alloc] initWithObjects: @"0", @"1", @"2",..., @"19",nil];
number = @"0" ;
numberCount++ ;
self.dayArray = array;
[array release];
label.text = [NSString stringWithFormat:@"Day: %@ ", [dayArray objectAtIndex :numberCount ]];
}
//and the timer
- (void)viewDidLoad
{
timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我的答案:
1)我认为,在最后一个(19),numberCount是20(numberCount++;)。
2)只需在调度定时器之前设置该值即可。
Here is my answers:
1)I think, at the last one (19), the numberCount is 20 (numberCount++ ;).
2)Just set the value before scheduling the timer.
将其添加到您的界面中的 .h 文件中(也就是说,如果它尚不存在)
然后在您的 viewDidLoad 方法中,初始化 numberCount 和标签:
在您的 time 方法中,替换:
为
What is the "number" NSString 用于,顺便说一句?
add this to your .h file, in your interface (that is, if it isn't already there)
Then in your viewDidLoad method, initialize numberCount and the label:
And in your time method, replace:
with
What is the "number" NSString used for, b.t.w.?
为什么要有dayArray?
为什么不像
我不知道你初始化的数字计数应该是 -1 并且也重新初始化为 -1 。如果您希望迭代“Day: 0”...“Day: 19”
目前还不清楚。
Why have dayArray?
why not something like
I don't know what you have number count initialized to it should probably be -1 and also reinitialized to -1 . if you wish to iterate thru "Day: 0" ... "Day: 19"
It's not clear.
您可以将 -(void)viewDidLoad 计时器更改为不重复
,如果您仍想在 5 秒后更改文本,则可以在rotate3 方法中有条件地再次设置它。
You could change the -(void)viewDidLoad timer to not repeat
then, conditionally set it up again in the rotate3 method if you still want to change the text 5 seconds later.
试试这个:
try this: