Objective c 如何使用每N秒更改NSString的值?
我使用此代码每五秒钟更改Objective c 如何使用每N秒更改NSString的值?,objective-c,cocoa,nsstring,nsarray,nstimer,Objective C,Cocoa,Nsstring,Nsarray,Nstimer,我使用此代码每五秒钟更改NSString中的数字 我如何保持数字循环运行?它现在从1运行到19,在最后一个(19)处停止,行中有一个SIGABRT:label.text=… 在第一个计时器启动之前,如何从显示的第一个数字(0)开始 代码如下: -(IBAction) rotate3 { NSString *number = [self.dayArray description]; NSArray *array = [[NSArray alloc] initWithObje
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];
}
以下是我的答案:
1) 我认为,在最后一个(19),numberCount是20(numberCount++;)。
2) 只需在安排计时器之前设置该值。将该值添加到界面中的.h文件中(即,如果该文件尚未存在) 然后在viewDidLoad方法中,初始化numberCount和标签:
numberCount = 0;
label.text = @"0";
在您的时间方法中,替换:
numberCount++
与
“数字”字符串用于什么,b.t.w.?为什么有dayArray?
为什么不像这样
label.text = [NSString stringWithFormat:@"Day: %d", numberCount++];
if (numberCount>19) numberCount = 0;
我不知道初始化为多少个数字,它可能应该是-1,也可能重新初始化为-1。如果您希望通过“日期:0”进行迭代。。。“日期:19”
不清楚。您可以将-(void)viewDidLoad计时器更改为不重复
timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:NO];
然后,如果5秒钟后仍要更改文本,请使用rotate3方法有条件地重新设置它。尝试以下操作:
#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.
}
我只是改变了这一点,但现在正在计算2,4,6,8…20,0在计时器方法中有重复的numberCount++行。移除其中一个。:-)哎呀。。。我忘了这里不是每个人都懂我们疯狂的美国俚语。意思是“顺便”或“另外”。谢谢迈克尔。现在工作得很好这就是代码的样子NSString*number=[self.dayArray description];NSArray*数组=[[NSArray alloc]initWithObjects:@“0”、“1”、“2”、“3”、“4”、“4”、“19”、“nil];number=[dayArray objectAtIndex:numberCount];numberCount++;label.text=@“0”;如果(numberCount>19)numberCount=0;label.text=[NSString stringWithFormat:@“Day:%@,[dayArray objectAtIndex:numberCount]];self.dayArray=数组;[阵列释放];数字NSString用于显示Maya月份中的天数。他们用0到19来计算天数。其他人对此投了反对票,但我投了反对票,因为@antonio的观点很重要。在计时器方法(每次)中设置这样的数组效率极低。最好只创建一次数组(例如,在
viewDidLoad
方法中)。谢谢Antonio,我会尝试让您知道。我按照您建议的方式尝试完代码,效果很好,只添加了NSString stringWithFormat…在setText之后:谢谢AntonioIn Maya日历日计数从0到19使用它,数字会变得疯狂
timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(rotate3 )userInfo:nil repeats:NO];
#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.
}