如何将文本字段值带入xcode Obj c中的其他viewController?

时间:2017-01-05 02:53:45

标签: php ios mysql objective-c xcode

我在PHP MySQL数据库和PHP代码中有电子邮件=' abc' pw =' 123' 值下方。

login2.php

<?php
$connect = mysqli_connect("localhost","","","");
if(isset($_POST['email'])) {
    $email = $_POST['email'];
    $pw    = $_POST['pw'];
    $sql   = "SELECT * FROM table WHERE email='$email' AND pw=md5('$pw')";
    $get   = mysqli_query($connect,$sql);

    if($get && mysqli_num_rows($get) > 0) {
        while($row = mysqli_fetch_assoc($get)) {
            $email_db = $row['email'];
            $pw_db    = $row['pw'];             
            echo '{"success":1}';
            exit();
        }
        mysqli_free_result($get);
    } else {
        echo '{"success":0}';
        echo mysqli_error($connect);
        exit();
    }
}
?>

我想在xcode中实现的是将成功值带入名为 HomepageVC 的其他ViewController中,并在单击loginbutton操作时显示该值。

现在我只能在同一个ViewController页面上查看文本字段值。我不知道如何将输入值传输到另一个ViewController。对象代码如下。

ViewController.m

@synthesize displayL,passL,email,pw;

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

        if([[email text] isEqualToString:@""] || [[pw text] isEqualToString:@""] ) {
            [self alertFailed:@"Please enter both Username and Password" :@"Login Failed!"];
        }
        else {
            NSString *post =[[NSString alloc] initWithFormat:@"email=%@&pw=%@",[email text],[pw text]];
            NSLog(@"PostData: %@",post);
            NSURL *url=[NSURL URLWithString:@"http://localhost/login2.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];

            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!"];
                    NSString * input  = email.text;
                    NSString * input2 = pw.text;
                    displayL.text = input;
                    passL.text = input2;

                } else {
                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertFailed:error_msg :@"Login failed"];
                }
            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertFailed:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想使用currentViewControllernextViewController的文字字段值,可以使用NSUserDefaults

将文本字段值存储在一个字符串中。例如Txtfldstring

Txtfldstring= [NSString stringWithFormat:@"%@",_yourTextField.text];

然后按以下方式存储该文本字段值:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString

[prefs setObject:Txtfldstring forKey:@"abc"];

在nextViewController或任何其他控制器中使用此值,如下所示;

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *s = [prefs stringForKey:@"abc"];
NSLog(@“%@",s);