0

I am new to iOS and I am not able to pass data from one controller to another. I am not able to access the variable in the second view controller

this is my method for passing I have created a delegate in .h file of receiving view controller

.h file of first view controller (sending) @interface OtpViewController : UIViewController @property (nonatomic,strong) NSString *str; @property (strong,nonatomic) NSString *tmp; @property(weak,nonatomic) NSString *requestReply ;

.m file of first view controller(sending)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
    VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = self.tmp;
NSLog(@"--%@",self.tmp);
[loadCtr setotp:self.tmpdict withMobile:_mobiletf.text];
//NSLog(@"otp:%@",[tmpdict valueForKey:@"otp"]);
NSLog(@"mobile:%@",_mobiletf.text);
}

.m file of second view controller(receiving)

-(void)setotp:(NSDictionary *)dic withMobile:(NSString *)str{
self.stri=[tmpdict valueforkey:@"otp"];
self.stri1=_mobiletf.text;
OtpViewController.[tmpdict valueforkey:@"otp"]=self.stri;
NSLog(@"%@----%@",self.stri,self.stri1);
}

.h file of second view controller(receiving)

@protocol VerifyViewControllerDelegate <NSObject>
@end

@interface VerifyViewController : UIViewController
@property (nonatomic,strong) NSString *otpStr;
@property(nonatomic,strong) NSString *mobileStr;

@end

actually I am trying to get otp from server and I have extracted the otp in the first view controller and now I have to pass otp and the mobile number from the text field to second view controller for verification of the otp please help!!

 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
         //   NSError *error;
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
            NSLog(@"requestReply: %@", jsonDict);
            self.tmp=[jsonDict valueForKey:@"otp"] ;
            self.str=self.tmp;
            NSLog(@"tmp storage inside block:%@",self.tmp);
    }] resume];
    [ self performSegueWithIdentifier:@"b1" sender:self];
    NSLog(@" storage:%@",self.str);
    NSLog(@"tmp storage:%@",self.tmp);
}

at log whatever is printed which is out of resume gives me null

this is my log data

2017-06-01 12:26:45.803 MenuBar[2652:124758] 9047038606
2017-06-01 12:26:45.809 MenuBar[2652:124758]  storage:(null)
2017-06-01 12:26:45.810 MenuBar[2652:124758] tmp storage:(null)
2017-06-01 12:26:48.422 MenuBar[2652:124804] requestReply: {
otp = 325106;
success = 1;
}
2017-06-01 12:26:48.422 MenuBar[2652:124804] tmp storage inside block:325106
2

2 Answers 2

2

Use Below code:

@interface VerifyViewController : UIViewController
@property (nonatomic,strong) NSString *otpStr;
@property(nonatomic,strong) NSString *mobileStr;

Then pass values:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender
{
    VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
     loadCtr.otpStr = [tmpdict valueForKey:@"otp"];
     loadCtr.mobileStr = _mobiletf.text;
}

You can access these 2 values in ViewDidLoad method of VerifyViewController.

self.otpStr and self. mobileStr

Sign up to request clarification or add additional context in comments.

13 Comments

it is saying tempdict:use od undeclared identifier but. @property (strong,nonatomic) NSDictionary *tmpdict; I have given this in .m file
and at log after session url if I print the value it shows null
[tmpdict valueForKey:@"otp"]; replace this with the parameter from which you are getting OTP value.
You can upvote or mark my ans as correct ans.. Please edit code as well which is correct..
can you tell me whats the problem??
|
1

(As of Xcode Version 9.4.1 (9F2000) - iOS 11.4 - Objective C) 
Most will need to call several storyboard segue calls from a single UIViewController. Here is how to handle the different segue string identifiers within the preparForSegue delegate call.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:  @“CallerViewController_ToMusicPlayerViewController_Segue"])
    {
        MusicPlayerViewController * targetVC = (MusicPlayerViewController *)segue.destinationViewController;
        targetVC.listName = @“Music Hits”;
    }
    if ([segue.identifier isEqualToString: @“CallerViewController_ToMusicListViewController_Segue"])
    {
        MusicListViewController * targetVC = (MusicListViewController *)segue.destinationViewController;
        // if you need to manage a protocol delegate with caller VC 
         targetVC.savefolderChoiceDelegate = self;
    }
}

The actual call to invoke the segue looks like:

-(void)buttonAction:(id)sender{
    [self performSegueWithIdentifier:@“CallerViewController_ToMusicPlayerViewController_Segue" sender:sender];
}

Here is an example of Interface Builder Segue. the identifier is different, but imagine @“CallerViewController_ToMusicPlayerViewController_Segue" is actually @"ProtData_Add_Segue" in this image.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.