如何在不单击任何按钮的情况下将firstViewController导航到secondViewController

时间:2016-05-12 07:46:53

标签: ios objective-c arrays

我需要知道如何在5秒后从firstViewController导航到secondViewController。如何定义自动移动的时间,而无需单击任何按钮 我是iOS开发的新手。我写了这段代码,但收到错误。请帮忙。提前致谢!

  // firstViewController.h

#import <UIKit/UIKit.h>
#import "secondViewController.h"//This import is not necessary
#import "firstViewController.h"

@interface firstViewController : UIViewController
  @property (strong, nonatomic) SecondViewController *secondViewController; //This instance is not necessary
@end

- (void)viewDidLoad 
{
  [super viewDidLoad];
  [self performSelector:@selector(loadingNextView)withObject:nil afterDelay:5.0f];
}

- (void)loadingNextView
{
  secondViewController = [[secondViewController alloc] init];
  [self.navigationController pushViewController:secondViewController animated:YES];
}

@end

4 个答案:

答案 0 :(得分:0)

试试这个

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(yourMethod) userInfo:nil repeats:NO];

答案 1 :(得分:0)

performSelector:withObject:afterDelay:

Documentation

以后使用performSegueWithIdentifier

示例:

[self performSelector:@selector(goToSecondController) withObject:nil afterDelay:5.0];

- (void)goToSecondController {
    [self performSegueWithIdentifier:@"segueInYourStoryboard" sender:self];
}

答案 2 :(得分:0)

请尝试我的代码可能有帮助 如果您使用.xib,则使用initWithNibName,否则使用init

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(push) userInfo:nil repeats:NO];

- (void)push
{
    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];;
    [self.navigationController pushViewController:secondViewController animated:YES];
}

答案 3 :(得分:0)

如果你正在使用UIStoryBoard,那么从第一个到第二个创建一个segue。然后执行performSegueWithIdentifier:sender:

如果没有,请不要担心。只需使用dispatch_after并在那里编写推送代码即可。它将在特定时间后执行。

无需使用NSTimerperformSelector:afterDelay:您可以使用dispatch_after安全地执行此操作。