Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在NSArray中显示每个对象2秒钟_Iphone_Ios_Objective C_Xcode - Fatal编程技术网

Iphone 在NSArray中显示每个对象2秒钟

Iphone 在NSArray中显示每个对象2秒钟,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,我有以下问题:我有一个包含5个对象的NSArray。我想使用NSTimer显示每个项目2秒钟,并在显示阵列中的最后一个项目后释放计时器 Country = [NSArray arrayWithObjects:@"Aus",@"India",@"Pak",@"China",@"Japan", nil]; cntcount = [Country count]; NSLog(@"concount=%i", cntcount); [NSTimer scheduledTimerWithTimeInte

我有以下问题:我有一个包含5个对象的
NSArray
。我想使用
NSTimer
显示每个项目2秒钟,并在显示阵列中的最后一个项目后释放计时器

Country = [NSArray arrayWithObjects:@"Aus",@"India",@"Pak",@"China",@"Japan", nil];

cntcount = [Country count];
NSLog(@"concount=%i", cntcount);

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(startTime) userInfo:nil repeats:YES];
但我不会再走一步了。在
starttime()
函数中应该做什么?请帮忙

//
//  AppViewController.m
//  NSTimerExample
//

#import "AppViewController.h"

@interface AppViewController ()

@end

@implementation AppViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    Country = [NSArray arrayWithObjects:
                  @"Aus", @"India", @"Pak", @"China", @"Japan", nil];

    tempValue = 0; 
    NSTimer *popupTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                                           target:self
                                                         selector:@selector(startTime:)
                                                         userInfo:nil
                                                          repeats:YES];
    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:popupTimer forMode:NSDefaultRunLoopMode];

}

- (void)startTime:(NSTimer *)theTimer {
    int cntcount = [Country count];
    if(tempValue < cntcount) {

        NSString *objectName = [Country objectAtIndex:tempValue];
        NSLog(@"objectName=%@", objectName);

        tempValue++;
    } else {
        [theTimer invalidate];
        theTimer = nil;
        // or you can reset tempValue to 0.

    }
}
//
//AppViewController.m
//NSTimerExample
//
#导入“AppViewController.h”
@接口AppViewController()
@结束
@AppViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
国家=[NSArray ARRAY WITH OBJECTS:
@“澳大利亚”,“印度”,“巴基斯坦”,“中国”,“日本”,无];
tempValue=0;
NSTimer*popupTimer=[NSTimer scheduledTimerWithTimeInterval:2.0
目标:自我
选择器:@选择器(开始时间:)
用户信息:无
重复:是];
NSRunLoop*runner=[NSRunLoop currentlunloop];
[runner addTimer:popupTimer forMode:NSDefaultRunLoopMode];
}
-(无效)开始时间:(n计时器*)计时器{
int cntcount=[国家/地区计数];
if(tempValue
显示最后一个项目后,我想释放计时器,因为它不再需要,但我得到了一个异常

.h

int tempValue;
.m

tempValue=0;     
NSTimer *popupTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(startTime:) userInfo:nil repeats:YES];;
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:popupTimer forMode: NSDefaultRunLoopMode];
在定时器动作方法中:

-(void)startTime:(NSTimer*)theTimer {
    if(tempValue< Country.count) {

         // Display array object eg: label.text=[Country objectAtIndex:tempValue];

         tempValue++;
    } else {
        [theTimer invalidate];
        theTimer=nil;
        // or you can reset the tempValue into 0.

    }
}
-(无效)开始时间:(n计时器*)计时器{
if(tempValue
#导入“AppViewController.h”
@接口AppViewController()
@结束
@AppViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
Country=[[NSArray alloc]initWithObjects:@“Aus”、@“India”、@“Pak”、@“China”、@“Japan”、nil];
tempValue=0;
NSTimer*popupTimer=[NSTimer scheduledTimerWithTimeInterval:2.0目标:自选择器:@selector(startTime:)用户信息:无重复:是];;
NSRunLoop*runner=[NSRunLoop currentlunloop];
[runner addTimer:popupTimer forMode:NSDefaultRunLoopMode];
}
-(无效)开始时间:(n计时器*)计时器{
if(tempValue

感谢Tony,此代码工作正常

您的
startTime
方法是什么样子的?我的startTime功能将在2.0秒后显示每个coutry name,如果控件到达最后一个对象,请停止计时器函数调用为什么需要释放NSTimer,因为您需要它来继续工作。显示最后一个对象名称后,我需要释放nstimer@Sandeep释放后是否要重新启动计时器?如果使用
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
计时器已在默认模式下在当前运行循环上调度。无需再次将其添加到运行循环中。您在if循环中做了什么???还要检查Country数组是否在timer方法中包含对象。@Sandeep我已经复制了您的代码并粘贴到我的应用程序中并进行了检查。这对我来说很好。。。。
#import "AppViewController.h"

@interface AppViewController ()

@end

@implementation AppViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    Country=[[NSArray alloc]initWithObjects:@"Aus",@"India",@"Pak",@"China",@"Japan", nil];
    tempValue=0; 
    NSTimer *popupTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(startTime:) userInfo:nil repeats:YES];;
    NSRunLoop *runner = [NSRunLoop currentRunLoop];
    [runner addTimer:popupTimer forMode: NSDefaultRunLoopMode];

}

- (void)startTime:(NSTimer*)theTimer {
    if(tempValue< Country.count) {

        NSString *objectname=[Country objectAtIndex:tempValue];
        NSLog(@"objectname is %@",objectname);       
        tempValue++;
    } else {
        [theTimer invalidate];
        theTimer=nil;
        // or you can reset the tempValue into 0.

    }
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end