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
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
(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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.