将NSString格式的数据从LoginViewController传递到FirstViewController

时间:2013-06-24 18:00:59

标签: objective-c ios6

LoginViewController是我的初始视图控制器。电子邮件地址是LoginViewController中的输入,我正在尝试将其发送到FirstViewController。我在这里发布了很多解决方案,但找不到我的答案的链接是不成功的。 请看看我的代码并告诉我哪里出错了。几天后我就陷入困境。我的主要问题是,当我打印它以查看它是否被携带到FirstViewController时,emailString的输出显示为null。

仅供参考 - 我正在使用故事板。我有一个标签栏控制器,它有三个标签,FirstViewController是第一个标签页。

LoginViewController.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

LoginViewController.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

FirstViewController.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;

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

故事板的屏幕截图: enter image description here

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

答案 2 :(得分:0)

我得到了解决方案。

只有三行需要我在prepareForSegue中添加这三个,我的程序正在运行

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

谢谢大家!