shouldPerformSegueWithIdentifier问题

时间:2014-04-28 13:05:18

标签: ios uistoryboardsegue

我试图解决我的轰鸣声问题,但我失败了。请帮我解决这个问题。我有登录视图,在验证了id和密码后,我将它推送到下一个视图控制器。请检查以下图像。  enter image description here

问题 - 当Id和密码正确时,它会推送到下一个视图控制器,但是在点击2次登录按钮之后。

代码 -

ServiceManager.m

 -(void)initGetAppServiceRequestWithUrl:(NSString *)baseUrl onCompletion:  
 (ServiceCompletionHandler)handler
 {
     NSString *fullUrl = [NSString stringWithFormat:@"%@",[baseUrl 
            stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL 
            URLWithString:fullUrl]];
    [NSURLConnection sendAsynchronousRequest:(NSURLRequest *)request
                                   queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response,NSData *data,NSError *error)
   {
     if (error) {
         handler(nil,error);
        // NSLog(@"error = %@",error);
    }
     else
     { handler(data, nil);
       // NSLog(@"data = %@",data);
     }
  }];

}

JSONResponseHandler.m

 +(void)handleResponseData:(NSData *)responseData onCompletion:(JSONHandler)handler
 {
     if (responseData) {
         NSError *jsonParseError;
         NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData 
         options:kNilOptions error:&jsonParseError];
         if (!json) {
             handler(nil , jsonParseError);
         }
        else
        {
             handler (json , nil);
        }  
    }
}

ASKevrServiceManager.m

 -(void)login:(Login *)login completionHandler:(ServiceCompletionHandler)handler
 {
     NSString *loginUrl = [NSString    
     stringWithFormat:@"http://249development.us/johnsan/askever/login.php?
     login=%@&password=%@",login.emailAddr , login.password];
     [self initGetAppServiceRequestWithUrl:loginUrl onCompletion:^(id object, NSError 
      *error)
     {
        handler(object , error);
     }
    ];
  }

ASKevrOperationManager.m

  +(void)login:(Login *)login handler:(OperationHandler)handler
  {
       ASKevrServiceManager *serviceManager = [[ASKevrServiceManager alloc]init];
      [serviceManager login:login completionHandler:^(id object, NSError *error)
      {
          [JSONResponseHandler handleResponseData:object onCompletion:^(NSDictionary 
           *json , NSError *jsonError)
          {
            if(json)
            {
                handler(json , nil , YES);
             }
            else
            {
               handler(nil , jsonError , NO);
            }
        }];
    }];
 }

LoginViewController.m

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
   if ([identifier isEqualToString:@"pushTab"])
   {
     if ([emailTxt.text isEqualToString:@""] || [passwordTxt.text 
     isEqualToString:@""])
     {
       [self showAlertWithMessage:@"Please write your id or password"];
       return NO;
  }
 else
 {
    Login *loginModel = [[Login alloc]init];
    loginModel.emailAddr =emailTxt.text;
    loginModel.password = passwordTxt.text;
    [ASKevrOperationManager login:loginModel handler:^(id object , NSError *error , 
    BOOL success)
     {
         if (success)
         {
             NSLog(@"object =%@",object);
             NSDictionary *arr = [object objectForKey:@"response"];
             str = [arr objectForKey:@"flag"];
             //check for error
                 NSDictionary *toDict = [object objectForKey:@"response"];
                 currentUserId = [toDict objectForKey:@"c_id"];
                 NSLog(@"currentUserId = %@",currentUserId);
         }
         else
         {
             [self showAlertWithMessage:@"Wrong Id or Password."];
         }
     }];
    NSLog(@"str = %@",str);
    if ([str isEqualToString:@"1"])
    {
       // [self showAlertWithMessage:@"Wrong Id or Password."];
        return YES;
    }
   }
 }
return NO;
}

1 个答案:

答案 0 :(得分:0)

按下登录按钮时,请运行代码

if (![emailTxt.text isEqualToString:@""] &&
    ![passwordTxt.text isEqualToString:@""]){
    Login *loginModel = [[Login alloc]init];
    loginModel.emailAddr =emailTxt.text;
    loginModel.password = passwordTxt.text;
    [ASKevrOperationManager login:loginModel handler:^(id object , NSError *error , 
    BOOL success)
     {
         if (success){
             NSLog(@"object =%@",object);
             NSDictionary *arr = [object objectForKey:@"response"];
             str = [arr objectForKey:@"flag"];
             //check for error
                 NSDictionary *toDict = [object objectForKey:@"response"];
                 currentUserId = [toDict objectForKey:@"c_id"];
                 NSLog(@"currentUserId = %@",currentUserId);

            //perform the segue only when succesful
            [self performSegueWithIdentifier:@"yourSegue" sender:sender];
         }else{
            [self showAlertWithMessage:@"Wrong Id or Password."];
         }
     }];    
}else {
    [self showAlertWithMessage:@"Please write your id or password"];
}

保持shouldPerformSegueWithIdentifier简单

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
   if ([identifier isEqualToString:@"pushTab"])
   {
        //don't put logic here
        //put code here only if you need to pass data
        //to the next screen
        return YES:
   }
   return NO;
}