0

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.
}

enter image description here see after three seconds how the loop is working .

Harshit Goel
  • 639
  • 1
  • 6
  • 19

5 Answers5

1

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];
    }
Usman Javed
  • 2,165
  • 1
  • 13
  • 25
0

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];
Gulliva
  • 468
  • 2
  • 10
  • Can you describe what happens? Also infinite loop? – Gulliva Jan 05 '17 at 08:21
  • Sir it moves to second VC after 1 sec but as it moves to 2nd it doesnt stop at 2nd it keeps on dismissing to 1st and then again moves to second, this happens again and again – Harshit Goel Jan 05 '17 at 08:24
  • So can you post the coding from the second Vc? I tried it, and it works for me. But the method has to be a little diffrent. I edited the Method... – Gulliva Jan 05 '17 at 08:27
0

It is easy to do, you can add a property to judge if is first come in the vc1:

The result:

enter image description here

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];
}
aircraft
  • 16,211
  • 16
  • 74
  • 135
  • http://stackoverflow.com/questions/41506009/objective-c-scroll-view-not-working-when-keyboard-appears-and-dissapears?noredirect=1#comment70217752_41506009 – Harshit Goel Jan 06 '17 at 15:52
0

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];
}
Usman Javed
  • 2,165
  • 1
  • 13
  • 25
0

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.
}
Harshit Goel
  • 639
  • 1
  • 6
  • 19