2017-06-01 138 views
0

我是iOS的新手,我无法将数据从一个控制器传递到另一个控制器。我不能够访问在所述第二视图控制器iOS客观c在视图控制器之间传递数据

变量这是我传球方法我已经建立在接收视图控制器h文件委托

第一个视图控制器

h文件(发送) @interface OtpViewController:UIViewController @property(nonatomic,strong)NSString * str; @property(strong,nonatomic)NSString * tmp; @property(弱,非原子)NSString * requestReply;第一视图控制器(接收的)第二视图控制器

-(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文件

.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文件(接收)

@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

请参阅链接https://www.infragistics.com/community/blogs/torrey-betts/archive/2014/05/29/passing-data-between-view-controllers-ios-obj-c。 aspx或https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – user3575114

+0

[视图控制器之间传递数据]的可能重复(https://stackoverflow.com/questions/5210535/passing-data视图间控制器) – Lion

回答

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方法中访问这2个值。

self.otpStr和self。 mobileStr

+0

它说tempdict:使用od未声明的标识符,但。 @property(强,非原子)NSDictionary * tmpdict; 我已经在.m文件 – Akshay

+0

中给出了这个以及在会话url之后的日志,如果我打印它显示的值为空 – Akshay

+0

[tmpdict valueForKey:@“otp”];将其替换为从中获取OTP值的参数。 –

相关问题