Question

What I want do is to pass data between view controllers once the login button is clicked, for this purpose I am using -prepareForSegue:sender:.

The problem:

I am not able to use uide in the method -prepareForSegue:sender: it says that uide is not declared. But declared in IBAction like this :

NSString *uide = jsonData[@"uid"];

 -(IBaction)login:(id)sender {        
  //some code here  
 @try {

 if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization
                                      JSONObjectWithData:urlData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];

            success = [jsonData[@"success"] integerValue];
            NSString *uide = jsonData[@"uid"];

            if(success == 1)
            {
                 [self performSegueWithIdentifier:@"segue" sender:self];
            } else {

                NSString *error_msg = (NSString *) jsonData[@"error_message"];
                [self alertStatus:error_msg :@"Sign in Failed!" :0];
            }             
        }
    }
    }
  }  
@catch (NSException * e) {
      NSLog(@"Exception: %@", e);
    [self alertStatus:@"Sign in Failed." :@"Error!" :0];
    }

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"segue"]) {
   FirstViewController *secondVC = (FirstViewController *)segue.destinationViewController;
    secondVC.uID = uide; //here I have declaration error, how can I make like an inheritance from the previous method ?
    NSLog(@"uid is : %@",secondVC.uID);
}}

I have been trying to solve this for days, but no solution found! Any ideas?

Was it helpful?

Solution

in your IBAction :

[self performSegueWithIdentifier:@"segue" sender:uide];

Then :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *uide = (NSString *)sender;

if ([segue.identifier isEqualToString:@"segue"]) {
   FirstViewController *secondVC = (FirstViewController *)segue.destinationViewController;
    secondVC.uID = uide; //here I have declaration error, how can I make like an inheritance from the previous method ?
    NSLog(@"uid is : %@",secondVC.uID);
}} 

OTHER TIPS

You have declared NSString *uide locally in your IBAction method. If you want to use it in another place, that variable have to be declared globally, for example, in your .h:

@property NSString *uide;

can't you just:

[self performSegueWithIdentifier:@"segue" sender:uide]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top