Objective c iOS 传递数据
Objective c iOS passing data
嗨,我是 iOS 的新手,下面是我的代码,我必须通过从请求回复中获得的顶部...请帮助我,我被困在这里
第一个视图控制器是 otp 视图第二个 vc 是 verifyviewcontroller
Otp 查看c.h
#import <UIKit/UIKit.h>
@interface OtpViewController : UIViewController
@property (nonatomic, retain) NSString *str;
@end
otpviewcontroller.m
#import "OtpViewController.h"
#import "VerifyViewController.h"
@interface OtpViewController () <VerifyViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bbi;
@property (weak, nonatomic) IBOutlet UIButton *submittf;
@property (weak, nonatomic) IBOutlet UITextField *mobiletf;
@property (weak,nonatomic) NSArray *tmp;
@property(weak,nonatomic) NSString *requestReply ;
//@property(weak,nonatomic) NSDictionary *A;
@end
@implementation OtpViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSLog(@"viewDidLoad");
[super viewDidLoad];
}
- (IBAction)submitb:(id)sender
{
if (_mobiletf.text && _mobiletf.text.length >0 )
{
/* not empty - do something */
NSString *post = [NSString stringWithFormat:@"phone=%@",_mobiletf.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.sitesandflats.com/send_otp.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSLog(@"the data Details is %@", post);
// And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
NSString *str=requestReply;
VerifyViewController *vc = [[VerifyViewController alloc] init];
NSString *tmp=requestReply;
NSLog(@"%@",str);
NSLog(@"%@",_tmp);
}] resume];
[ self performSegueWithIdentifier:@"b1" sender:self];
}
else
{
/* what ever */
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please check your input!!."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = (@"otp: %@",_tmp);
NSLog(@"passing val: %@",_tmp);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
验证视图 controller.h
#import <UIKit/UIKit.h>
@protocol VerifyViewControllerDelegate <NSObject>
-(void)moveToA:(NSString *)str;
@end
@interface VerifyViewController : UIViewController
@property (nonatomic, copy) NSString *tmpStr;
@property (nonatomic, assign) id <VerifyViewControllerDelegate> delegate;
//@property (nonatomic, strong) OtpViewController *received;
//@property (strong, nonatomic) IBOutlet UITextField *textDisplay;
@property(nonatomic,weak) NSMutableArray *myAray;
@property(nonatomic,strong) NSString *object;
@property(strong,nonatomic) NSString *tmp1;
@end
验证视图 controller.m
#import "VerifyViewController.h"
#import "OtpViewController.h"
@interface VerifyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *otptf;
@property (weak, nonatomic) IBOutlet UIButton *submitb;
//@property (weak,nonatomic) textDisplay;
//@property (weak,nonatomic) NSString *received;
@end
@implementation VerifyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
// Do any additional setup after loading the view.
}
- (IBAction)submitb:(id)sender {
NSLog(@"%@",_tmpStr);
if(_otptf.text==_tmpStr)
{
[self performSegueWithIdentifier:@"b2" sender:self];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Incorrect otp please check the input!!!."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在 nslog 我得到回复 ("success":"1";"otp":"985123") 我需要这个 otp 来存储并在下一页验证请帮助
首先你需要改变属性
来自
@property (weak,nonatomic) NSArray *tmp;
至
@property (strong,nonatomic) NSStirng *tmp;
将值赋给 tmp,如下所示
[[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"] ; // you need to extract value using the key [requestReply valueForKey:@"otp"];
NSLog(@"%@",self.tmp);
}] resume];
将 tmp 传递给 VerifyVIewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = self.tmp;
NSLog(@"passing val: %@",_tmp);
}
嗨,我是 iOS 的新手,下面是我的代码,我必须通过从请求回复中获得的顶部...请帮助我,我被困在这里 第一个视图控制器是 otp 视图第二个 vc 是 verifyviewcontroller
Otp 查看c.h
#import <UIKit/UIKit.h>
@interface OtpViewController : UIViewController
@property (nonatomic, retain) NSString *str;
@end
otpviewcontroller.m
#import "OtpViewController.h"
#import "VerifyViewController.h"
@interface OtpViewController () <VerifyViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bbi;
@property (weak, nonatomic) IBOutlet UIButton *submittf;
@property (weak, nonatomic) IBOutlet UITextField *mobiletf;
@property (weak,nonatomic) NSArray *tmp;
@property(weak,nonatomic) NSString *requestReply ;
//@property(weak,nonatomic) NSDictionary *A;
@end
@implementation OtpViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSLog(@"viewDidLoad");
[super viewDidLoad];
}
- (IBAction)submitb:(id)sender
{
if (_mobiletf.text && _mobiletf.text.length >0 )
{
/* not empty - do something */
NSString *post = [NSString stringWithFormat:@"phone=%@",_mobiletf.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Next up, we read the postData's length, so we can pass it along in the request.
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.sitesandflats.com/send_otp.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSLog(@"the data Details is %@", post);
// And finally, we can send our request, and read the reply by creating a new NSURLSession:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
NSString *str=requestReply;
VerifyViewController *vc = [[VerifyViewController alloc] init];
NSString *tmp=requestReply;
NSLog(@"%@",str);
NSLog(@"%@",_tmp);
}] resume];
[ self performSegueWithIdentifier:@"b1" sender:self];
}
else
{
/* what ever */
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please check your input!!."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = (@"otp: %@",_tmp);
NSLog(@"passing val: %@",_tmp);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
验证视图 controller.h
#import <UIKit/UIKit.h>
@protocol VerifyViewControllerDelegate <NSObject>
-(void)moveToA:(NSString *)str;
@end
@interface VerifyViewController : UIViewController
@property (nonatomic, copy) NSString *tmpStr;
@property (nonatomic, assign) id <VerifyViewControllerDelegate> delegate;
//@property (nonatomic, strong) OtpViewController *received;
//@property (strong, nonatomic) IBOutlet UITextField *textDisplay;
@property(nonatomic,weak) NSMutableArray *myAray;
@property(nonatomic,strong) NSString *object;
@property(strong,nonatomic) NSString *tmp1;
@end
验证视图 controller.m
#import "VerifyViewController.h"
#import "OtpViewController.h"
@interface VerifyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *otptf;
@property (weak, nonatomic) IBOutlet UIButton *submitb;
//@property (weak,nonatomic) textDisplay;
//@property (weak,nonatomic) NSString *received;
@end
@implementation VerifyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
// Do any additional setup after loading the view.
}
- (IBAction)submitb:(id)sender {
NSLog(@"%@",_tmpStr);
if(_otptf.text==_tmpStr)
{
[self performSegueWithIdentifier:@"b2" sender:self];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Incorrect otp please check the input!!!."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在 nslog 我得到回复 ("success":"1";"otp":"985123") 我需要这个 otp 来存储并在下一页验证请帮助
首先你需要改变属性
来自
@property (weak,nonatomic) NSArray *tmp;
至
@property (strong,nonatomic) NSStirng *tmp;
将值赋给 tmp,如下所示
[[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"] ; // you need to extract value using the key [requestReply valueForKey:@"otp"];
NSLog(@"%@",self.tmp);
}] resume];
将 tmp 传递给 VerifyVIewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = self.tmp;
NSLog(@"passing val: %@",_tmp);
}