如何在IBAction中调用PrepareForSegue?

时间:2014-03-11 08:51:25

标签: ios segue ibaction

我想要做的是在点击登录按钮后在视图控制器之间传递数据,为此我使用-prepareForSegue:sender:

问题:

我无法在方法uide中使用-prepareForSegue:sender:,因为它表示未声明uide。但在IBAction中声明如下:

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

我一直在努力解决这个问题,但没有找到解决方案!有什么想法吗?

3 个答案:

答案 0 :(得分:3)

在您的IBAction中:

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

然后:

- (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);
}} 

答案 1 :(得分:0)

您已在IBAction方法中本地声明了NSString * uide。如果要在其他地方使用它,则必须全局声明该变量,例如,在.h中

@property NSString *uide;

答案 2 :(得分:0)

不能只是:

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