-3

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?

user3405229
  • 85
  • 4
  • 12
  • are you aware of the `-prepareForSegue:sender:` method and `– performSegueWithIdentifier:sender:` method are two **different** ones? – holex Mar 11 '14 at 08:56

3 Answers3

3

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);
}} 
Armand DOHM
  • 1,121
  • 1
  • 7
  • 9
  • I got this error which is caused I think by : `sender:uide` – user3405229 Mar 11 '14 at 09:04
  • As you can see in my code, I have if else statement. It goes to else which is (Sign in failed !) – user3405229 Mar 11 '14 at 09:08
  • @user3405229 then problem is in `success = [jsonData[@"success"] integerValue];` this line check what is the value of success and why as it not `1` depends on you logic. – Buntylm Mar 11 '14 at 09:11
  • Oh ! you never call `[self performSegueWithIdentifier:@"segue" sender:self];` and you want to get some data in `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender`. Just impossible ! You need to debug the part up to the part of the code ! One more thing is, what is responseData ? you never use it... – Armand DOHM Mar 11 '14 at 09:15
  • I got this when putting your code : `-[UITabBarController setUID:]: unrecognized selector sent to instance 0x1da45220 Exception: -[UITabBarController setUID:]: unrecognized selector sent to instance 0x1da45220 ` – user3405229 Mar 11 '14 at 09:17
  • I've just updated my code, the error is caused by `@catch (NSException * e)` can you please tell me why ? – user3405229 Mar 11 '14 at 09:21
  • Can you debug step by step and let us know when this happen ? – Armand DOHM Mar 11 '14 at 09:21
  • Did you declare the property uID in your secondVC ? This should not be an iBoutlet, and should be set as strong ! – Armand DOHM Mar 11 '14 at 09:59
0

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;
EnriMR
  • 3,924
  • 5
  • 39
  • 59
0

can't you just:

[self performSegueWithIdentifier:@"segue" sender:uide]
Mou某
  • 556
  • 3
  • 10
  • 32