I am trying to build a splash screen so I want the 1st View Controller to move to 2nd ViwController automatically after 3.0 sec I have tried the below method but an infinite loop has started what should i do ,how should I stop on second view controller.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%p", self);
NSLog(@"1st Controller");
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
});
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
@interface LoginViewController : ViewController
@end
//LoginViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
see after three seconds how the loop is working .
Why don't you use NSTimer
class. Simply create a timer for 10 seconds when 10 seconds passed timer will trigger the event and in that event you can move to another controller
. Create a timer like this
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:NO];
- (void)targetMethod:(NSTimer*)timer {
[self loadingNextView];
}
It is easy to do, you can add a property to judge if is first come in the vc1:
The result:
In fitst VC:
#import "ViewController.h"
#import "LoginViewController.h"
@interface ViewController ()
@property(nonatomic, assign) BOOL isFirstTime;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_isFirstTime = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
/* [self performSelector:@selector(loadingNextView)
withObject:nil afterDelay:1.0f]; */
if (_isFirstTime == NO) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self loadingNextView];
_isFirstTime = NO;
});
}
- (void)loadingNextView{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
Try this
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:false block:^(NSTimer * _Nonnull timer) {
[self loadingNextView];
}];
So a little advise, you should think about presenting a loginViewController instead pushing it to the NavigationController stack. If you push it, you have to remove the back buttons if you don't want the user to come back to the other ViewController. If you present it you can be sure that the user can't go back to the firstVc without entering his login data.
In the second Vc you then can dismiss the Vc or you can present a new ViewController.
[self presentViewController:vc animated:YES completion:nil];
You can achieve you goal by using this.
#import "DrawingViewController.h"
#import "SecondViewController.h"
@interface DrawingViewController ()
@end
@implementation DrawingViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[button removeFromSuperview];
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(target:) userInfo:nil repeats:NO];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)target:(NSTimer*)timer {
SecondViewController* controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
Finally got the answer.
#import "ViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%p", self);
[self.navigationController setNavigationBarHidden:YES animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"1st controller");
[self loadingNextView];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadingNextView{
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.navigationController pushViewController:viewController animated:true];
}
//LoginViewController.h
#import "ViewController.h"
@interface LoginViewController : UIViewController
@end
//LoginViewController.m
#import "LoginViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"2nd View Controller");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}