问题描述
我在 PHP MySQL 数据库和 PHP 代码中有这个 email = 'abc' 和 pw = '123' 值,如下所示.
I have this email = 'abc' and pw = '123' value in PHP MySQL database and PHP code as below.
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 中实现的是将 success 值带入其他名为 HomepageVC 的 ViewController 中,并在单击登录按钮操作时显示该值.
What I want to achieve in xcode is to bring the success value into other ViewController called HomepageVC and display the value when loginbutton action is clicked.
现在我只能在同一个 ViewController 页面上查看文本字段值.我不知道如何将输入值传输到另一个 ViewController.Obj c 代码如下.
Right now i can only view the textfield value on the same ViewController page. I don't know how to transfer the input value into another ViewController. Obj c code as below.
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!"];
}
}
推荐答案
如果你想使用从 currentViewController
到 nextViewController
的 textfield 的值,你可以使用 NSUserDefaults
.
If you want to use values of textfield from currentViewController
to nextViewController
, you can use NSUserDefaults
.
将您的文本字段值存储在一个字符串中.例如 Txtfldstring
store your textfield value in one string. for example Txtfldstring
Txtfldstring= [NSString stringWithFormat:@"%@",_yourTextField.text];
然后按照以下方式存储该文本字段值:
Then store that textfield value like following way:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:Txtfldstring forKey:@"abc"];
在 nextViewController 或任何其他控制器中使用此值,如下所示;
Use this value in nextViewController or any other Controller like following way;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *s = [prefs stringForKey:@"abc"];
NSLog(@"%@",s);
这篇关于如何将文本字段值带入 xcode Obj c 中的其他视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!