有两个viewcontroller,设置每隔几秒钟切换一次 求各位大神解答

时间:2022-05-24 20:34:51
例如有两个分别为A,B的viewcontroller ,然后每隔5秒钟重复切换两个viewcontroller。



    kangqijun 发表于 2013-7-25 15:16
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSel ...


-(void)screenFun:(NSTimer *)timer
{
   
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(sss) userInfo:nil repeats:YES];
    [time invalidate];
    time = nil;
}

-(void)sss
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
   
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
    static int i = 0;
    if (i % 2 == 0) {
        [self presentViewController:screenSaver animated:YES completion:nil];   
    }
    if (i % 2 == 1) {
        [[self parentViewController] presentViewController:screenSaverA animated:YES completion:nil];
    }
    i++;
    if (i>2) {
        i = 0;
    }
}



我写了上面的代码,可提示有警告还有就是一直显示screenSaverA这个视图。

4 个解决方案

#1


这样应该可以的,有问题加我Q:506341588


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}

static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    //screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
    
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    //screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
    
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

#2



- (void)viewDidLoad
{
    [super viewDidLoad];
     
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}
 
static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
     
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
     
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

#3


引用 2 楼 wangliangffaflgh 的回复:

- (void)viewDidLoad
{
    [super viewDidLoad];
     
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}
 
static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
     
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
     
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

可以正常运行 但是会报一个警告:Attempt to present <ViewController: 0x71b0b50> on <AnoViewController: 0x7564490> while a presentation is in progress! 

#4


需求: 做一个屏保。
有两个viewcontroller,一个是主页A,一个展示图片为B。
1,每个几秒就切换一次,例如显示一张图片,然后显示A,再显示另外一张图片,再显示A,就这样循环。
2,当点击图片的时,页面回到A;当点击A时候,屏保停止。  可以对A进行操作。








#1


这样应该可以的,有问题加我Q:506341588


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}

static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    //screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
    
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    //screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
    
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

#2



- (void)viewDidLoad
{
    [super viewDidLoad];
     
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}
 
static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
     
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
     
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

#3


引用 2 楼 wangliangffaflgh 的回复:

- (void)viewDidLoad
{
    [super viewDidLoad];
     
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(screenFun:) userInfo:nil repeats:YES];
}
 
static int i = 0;
static id viewController = nil;
-(void)screenFun:(NSTimer *)timer
{
    AdsfdViewController *screenSaverA = [[AdsfdViewController alloc] initWithNibName:@"AdsfdViewController" bundle:nil];
    screenSaverA.view.frame = CGRectMake(0, 0, 1024, 768);
     
    ScreenSaverViewController *screenSaver = [[ScreenSaverViewController alloc] initWithNibName:@"ScreenSaverViewController" bundle:nil];
    screenSaver.view.frame = CGRectMake(0, 0, 1024, 768);
     
    if (i % 2 == 0) {
        viewController = self;
        [viewController presentViewController:screenSaver animated:YES completion:^{
            viewController = screenSaver;
            i++;
            NSLog(@"S %d", i);
        }];
    }
    else {
        [viewController presentViewController:screenSaverA animated:YES completion:^{
            viewController = screenSaverA;
            i++;
            NSLog(@"A %d", i);
        }];
    }
}

可以正常运行 但是会报一个警告:Attempt to present <ViewController: 0x71b0b50> on <AnoViewController: 0x7564490> while a presentation is in progress! 

#4


需求: 做一个屏保。
有两个viewcontroller,一个是主页A,一个展示图片为B。
1,每个几秒就切换一次,例如显示一张图片,然后显示A,再显示另外一张图片,再显示A,就这样循环。
2,当点击图片的时,页面回到A;当点击A时候,屏保停止。  可以对A进行操作。