捕获可变内容并将其传递给第二个视图控制器

时间:2014-04-17 14:25:55

标签: objective-c

我已经阅读了很多关于在Xcode中的ViewController之间传递变量的主题,我完全迷失了。

1:我的服务器上有一个成员数据库,其中填充了成员详细信息

2:在xcode中有一个“ViewController.h和.m”,它运行代码,允许用户通过我的服务器上的成员数据表进行身份验证。这非常有效,没有问题。

我想要做的是将“登录”和“密码”捕获到两个变量中,并在第二个“ViewController”中使用变量。

First ViewController.h:

@interface FirstViewController : UIViewController {    

UITextField *txtUsername;
UITextField *txtPassword;    
} 

@property (nonatomic, retain)    IBOutlet UITextField *txtUsername;
@property (nonatomic, retain) IBOutlet UITextField *txtPassword;

- (IBAction)loginClicked:(id)sender;
- (IBAction)backgroundClick:(id)sender;

First ViewController.m

#import "FirstViewController.h"
#import "WebViewController.h"


#import "SBJson.h"

@implementation FirstViewController
@synthesize txtPassword, txtUsername;

- (IBAction)loginClicked:(id)sender {
@try {

    if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {




        [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
    } else {
       NSString *post =[[NSString alloc] initWithFormat:@"login=%@&pass=%@",[txtUsername text],[txtPassword text]];


        NSLog(@"PostData: %@",post);

        NSURL *url=[NSURL URLWithString:@"http://www.mydomain.com/check1.php"];


        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Response code: %d", [response statusCode]);
        if ([response statusCode] >=200 && [response statusCode] <300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);                    

            SBJsonParser *jsonParser = [SBJsonParser new];
            NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
            NSLog(@"%@",jsonData);
            NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];

            NSLog(@"%d",success);


            if(success == 1)
            {

                //NSLog(@"Login SUCCESS");
                //[self alertStatus:@"Logged in Successfully." :@"Login Success!"];

               WebViewController *second = [[WebViewController alloc] initWithNibName:nil bundle:nil];

                [self presentModalViewController:second  animated:YES];


            } else {

                NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                [self alertStatus:error_msg :@"Login Failed!"];
            }

        } else {
            if (error) NSLog(@"Error: %@", error);
            [self alertStatus:@"Connection Failed" :@"Login Failed!"];
        }
    }
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
    [self alertStatus:@"Login Failed." :@"Login Failed!"];
}
}

第二个ViewController.h

@interface LettoWebViewController : UIViewController {

IBOutlet UIWebView *lettoWebView;

 NSString *AmemberUsername;
 NSString *AmemberPassword;
}

@property (retain) NSString *AmemberUsername;
@property (retain) NSString *AmemberPassword;

第二个ViewController.m

#import "WebViewController.h"
#import "SBJson.h"


@interface WebViewController ()

@end


@implementation WebViewController

@synthesize AmemberPassword, AmemberUsername;

- (void)viewDidLoad
{


[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

NSString *post =[[NSString alloc] initWithFormat:@"login=%@&pass=%@",[AmemberUsername text],[AmemberPassword text]];
NSLog(@"PostData: %@",post);

NSURL *myURL=[NSURL URLWithString:@"http://www.mydomain.com/apps/index.php"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:myURL];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


[lettoWebView loadRequest:request];    


}

我的问题是;我怎样才能以最简单的方式传递这两个变量。

任何帮助都会很棒,但请善待我对xcode和编码很新。

此致

DJ

1 个答案:

答案 0 :(得分:0)

解决方案:
创建WebViewController时,您只需设置两个参数。

WebViewController *second = [[WebViewController alloc] initWithNibName:nil bundle:nil]; 
[second setAmemberUsername:[txtUsername text]]; 
[second setAmemberPassword:[txtPassword text]]; 
[self presentModalViewController:second  animated:YES];

viewDidLoad的{​​{1}}中: 有一个问题,你写道:

WebViewController

但是,NSString *post =[[NSString alloc] initWithFormat:@"login=%@&pass=%@",[AmemberUsername text],[AmemberPassword text]]; AmemberPasswordAmemberUserName。所以他们不知道NSString方法。他们不是text

所以纠正是:

UITextField