Objective C how to stop infinte loop while moving from first VC to 2nd

本小妞迷上赌 提交于 2019-12-25 07:49:18

问题


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 .


回答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];
    }



回答2:


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];
}



回答3:


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];



回答4:


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];
}



回答5:


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


来源:https://stackoverflow.com/questions/41479764/objective-c-how-to-stop-infinte-loop-while-moving-from-first-vc-to-2nd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!