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