2017-06-01 177 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值的參數。 –

相關問題