2

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;
    }   
}

故事板截图: 在此处输入图像描述

4

3 回答 3

1

如果您使用 StoryBoards,我强烈建议您使用 segues 来调用不同的视图控制器。这样你就可以相对容易地推送 vc 和传递信息。

您可以在 Login VC 中覆盖本机方法 prepareForSegue,并在那里设置属性。您可以将您的 segue id 命名为故事板中的任何内容。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      if ([[segue identifier] isEqualToString:@"firstVC"]) {
          FirstViewController *firstVC = [segue destinationViewController];
          firstVC.emailString = EmailField.text;
     }

 }

如果你真的想要,我建议不要使用静态变量是 NSUserDefaults。

 [[NSUserDefaults standardUserDefaults]
    setObject:EmailField.text forKey:@"emailString"];

稍后取回

      NSString *emailString = [[NSUserDefaults standardUserDefaults]
     stringForKey:@"emailString"];

       NSLog(@"%@",emailString);
于 2013-06-24T18:08:16.193 回答
0

我得到了解决方案。

我需要在 prepareForSegue 中添加的唯一三行就是这三行,我的程序正在运行

    FirstViewController* fvc = [[FirstViewController alloc] init];
    UITabBarController *tbc = [segue destinationViewController];
    fvc = (FirstViewController *) [[tbc customizableViewControllers] objectAtIndex:0];

感谢大家!

于 2013-06-24T19:12:17.610 回答
0

我会将电子邮件和密码安全地保存在 IOS 钥匙串中,而不是在视图之间传递。

https://developer.apple.com/library/ios/#documentation/security/conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html#//apple_ref/doc/uid/TP30000897-CH208-SW1

于 2013-06-24T18:14:03.810 回答