LoginViewController 是我的初始视图控制器。电子邮件地址是 LoginViewController 中的输入,我正在尝试将其发送到 FirstViewController。我经历了很多在这里发布的解决方案,但没有成功找到我的答案的链接。请查看我的代码并告诉我哪里出错了。几天后,我被困在这一点上。我的主要问题是,当我打印 emailString 的输出以查看它是否被传送到 FirstViewController 时,它的输出显示为 null。
仅供参考 - 我正在使用故事板。我有一个标签栏控制器,它有三个标签,FirstViewController 是第一个标签页。
登录视图控制器.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface LoginViewController : UIViewController{
NSString *email;
}
- (IBAction)LoginButton:(id)sender;
- (IBAction)CancelButton:(id)sender;
- (IBAction)dismissKeyboard:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *EmailField;
@property (weak, nonatomic) IBOutlet UITextField *PasswordField;
@end
登录视图控制器.m
#import "LoginViewController.h"
#import "FirstViewController.h"
#define USERNAME @"abc@gmail.com"
@interface LoginViewController ()
@end
@implementation LoginViewController
@synthesize EmailField;
@synthesize PasswordField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
email = [[NSString alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)LoginButton:(id)sender {
email = EmailField.text;
if([EmailField.text isEqualToString:USERNAME])
{
FirstViewController *fvc = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
fvc.emailString = [[NSString alloc] initWithFormat:@"%@",EmailField.text];
// fvc.emailString = email;
[self.navigationController pushViewController:fvc animated:YES];
}
[EmailField resignFirstResponder];
}
- (IBAction)CancelButton:(id)sender {
NSLog(@"Cancel button pressed!!!");
[EmailField resignFirstResponder];
[PasswordField resignFirstResponder];
}
- (IBAction)dismissKeyboard:(id)sender {
[EmailField resignFirstResponder];
[PasswordField resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self LoginButton:nil];
[textField resignFirstResponder];
return YES;
}
@end
第一视图控制器.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (nonatomic, retain) NSMutableData *receivedData;
@property (copy) NSString *emailString;
第一视图控制器.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize emailLabel;
@synthesize receivedData;
@synthesize emailString;
- (void)viewDidLoad
{
[super viewDidLoad];
[emailLabel setText: emailString];
NSString *theURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://.....email=%@",emailString]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:theURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(connection){
NSLog(@"connection successful");
NSLog(@"%@",emailString);
receivedData = [NSMutableData data];
}
else{
NSLog(@"connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receivedData setLength:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Success");
NSLog(@"Received %d bytes of data",[receivedData length]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
我也尝试在 LoginViewController 中实现 prepareFOrSegue 方法。但它仍然没有做出任何改变。这是我写在里面的代码。
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showDetailSegue"])
{
NSString *email = EmailField.text;
NSLog (@"++++++++++ %@", email);
FirstViewController *fvc = [segue destinationViewController];
fvc.emailString=email;
}
}
故事板截图: