我是iOS
的新手,我无法将数据从一个控制器传递到另一个控制器。我无法访问第二个视图控制器中的变量
这是我传递的方法我在接收视图控制器的.h文件中创建了一个委托
第一个视图控制器的.m文件的第一个视图控制器(发送)
- (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文件的第二个视图控制器(接收)
-(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文件的第二个视图控制器(接收)
@protocol VerifyViewControllerDelegate <NSObject>
@end
@interface VerifyViewController : UIViewController
@property (nonatomic,strong) NSString *otpStr;
@property(nonatomic,strong) NSString *mobileStr;
@end
实际上我正试图从服务器获取otp
并且我已经在第一个视图控制器中提取了otp
,现在我必须从文本字段传递otp
和手机号码到第二个视图控制器验证otp
请帮助!!
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);
}
在日志中打印出来的任何不在恢复状态的内容都是空的
这是我的日志数据
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
答案 0 :(得分:1)
使用以下代码:
@interface VerifyViewController : UIViewController
@property (nonatomic,strong) NSString *otpStr;
@property(nonatomic,strong) NSString *mobileStr;
然后传递值:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender
{
VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.otpStr = [tmpdict valueForKey:@"otp"];
loadCtr.mobileStr = _mobiletf.text;
}
您可以在VerifyViewController的ViewDidLoad方法中访问这两个值。
self.otpStr和self。 mobileStr
答案 1 :(得分:0)
(从Xcode版本9.4.1(9F2000)-iOS 11.4-目标C开始)ost大多数人将需要从单个UIViewController调用多个故事板segue调用。这是在preparForSegue委托调用中处理不同的segue字符串标识符的方法。
- (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;
}
}
调用segue的实际调用如下:
-(void)buttonAction:(id)sender{
[self performSegueWithIdentifier:@“CallerViewController_ToMusicPlayerViewController_Segue" sender:sender];
}
这里是Interface Builder Segue的一个示例。标识符不同,但是假设此图像中的@“ CallerViewController_ToMusicPlayerViewController_Segue”实际上是@“ ProtData_Add_Segue”。