登录用户使用 NSUserDefaults 执行 segue 不工作用户数据

Logged in user perform segue not working user data using NSUserDefaults

如果用户登录到主页并且连接已连接并且我已将标识符指定为 "loggedin",我实际上正在尝试执行连接 我目前还没有为注销编码,所以没有办法清除 NSUserDefaults 我也检查过它会恢复我的价值!!

下面是我的视图加载第一个视图控制器的方法,我正在从用户那里获取手机号码用于 otp

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    self.custid =[defaults objectForKey:@"uid"];
    NSLog(@"Customer id--%@",self.custid);
    if(self.custid!=nil)
    {
        [self performSegueWithIdentifier:@"loggedin" sender:self];
    }

    [_checkboxb setBackgroundImage:[UIImage imageNamed:@"unsigned checkbox"]
                         forState:UIControlStateNormal];
    [_checkboxb setBackgroundImage:[UIImage imageNamed:@"signed check box"]
                         forState:UIControlStateSelected];
    UIGraphicsBeginImageContext(self.view.frame.size);
}

这是我的故事板,在日志中你可以看到错误

我的那个视图控制器的代码正在执行,而不是登录正在执行的第一个视图,而是第一个视图控制器的视图

不要在第一个视图控制器中检查用户登录状态。检查 AppDelegate 中的登录状态并据此分配根视图控制器。

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
  UIViewController *viewController;
  NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  if([defaults objectForKey:@"uid"]!=nil)
  {
    viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
  }
  else
  {
    viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController1"];
  }
  self.window.rootViewController = viewController;//With out NavigationController
  //self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:viewController];//With NavigationController
  [self.window makeKeyAndVisible];
  return YES;
}