如何使用每 N 秒更改 NSString 的值?

发布于 2024-12-14 03:18:32 字数 755 浏览 2 评论 0原文

我有这段代码每五秒更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小伙你站住 2024-12-21 03:18:32

这是我的答案:

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.

思慕 2024-12-21 03:18:32

将其添加到您的界面中的 .h 文件中(也就是说,如果它尚不存在)

{
    NSInteger numberCount
}

然后在您的 viewDidLoad 方法中,初始化 numberCount 和标签:

numberCount = 0;
label.text = @"0";

在您的 time 方法中,替换:

numberCount++

if(numberCount++ > 19)
    numberCount = 0;

What is the "number" NSString 用于,顺便说一句?

add this to your .h file, in your interface (that is, if it isn't already there)

{
    NSInteger numberCount
}

Then in your viewDidLoad method, initialize numberCount and the label:

numberCount = 0;
label.text = @"0";

And in your time method, replace:

numberCount++

with

if(numberCount++ > 19)
    numberCount = 0;

What is the "number" NSString used for, b.t.w.?

还如梦归 2024-12-21 03:18:32

为什么要有dayArray?
为什么不像

label.text = [NSString stringWithFormat:@"Day: %d", numberCount++];
if (numberCount>19) numberCount = 0;

我不知道你初始化的数字计数应该是 -1 并且也重新初始化为 -1 。如果您希望迭代“Day: 0”...“Day: 19”
目前还不清楚。

Why have dayArray?
why not something like

label.text = [NSString stringWithFormat:@"Day: %d", numberCount++];
if (numberCount>19) numberCount = 0;

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.

淡忘如思 2024-12-21 03:18:32

您可以将 -(void)viewDidLoad 计时器更改为不重复

timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:NO];

,如果您仍想在 5 秒后更改文本,则可以在rotate3 方法中有条件地再次设置它。

You could change the -(void)viewDidLoad timer to not repeat

timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:NO];

then, conditionally set it up again in the rotate3 method if you still want to change the text 5 seconds later.

清风夜微凉 2024-12-21 03:18:32

试试这个:

#pragma mark - timer callback

-(IBAction)rotate3
{
    [label1 setText:[dayArray objectAtIndex:numberCount]];
    numberCount++;
    if (numberCount >= [dayArray count])
        numberCount = 0;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    dayArray = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3", nil];
    [label1 setText:[dayArray objectAtIndex:0]];
    numberCount = 1;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(rotate3) userInfo:nil repeats:YES];
    // Do any additional setup after loading the view, typically from a nib.
}

try this:

#pragma mark - timer callback

-(IBAction)rotate3
{
    [label1 setText:[dayArray objectAtIndex:numberCount]];
    numberCount++;
    if (numberCount >= [dayArray count])
        numberCount = 0;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    dayArray = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3", nil];
    [label1 setText:[dayArray objectAtIndex:0]];
    numberCount = 1;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(rotate3) userInfo:nil repeats:YES];
    // Do any additional setup after loading the view, typically from a nib.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文