-1

I am tryin to pass the user login emial to other view controllers. It is going from a UITextField to a UILabel but it display null when I log in a user. I want it to display the email name in the UILabel field. Any help? here are my files.

vc1.h

 #import <UIKit/UIKit.h>

@interface loginUserViewController : UIViewController


@property (nonatomic, strong) IBOutlet UITextField *email;
@property (nonatomic, retain) IBOutlet UITextField *password;
@property (nonatomic, retain) IBOutlet UIButton *login;
@property (nonatomic,retain) IBOutlet UIButton *registerBtn;


-(IBAction)loginUser:(id)sender;





@end

vc2.h

#import <UIKit/UIKit.h>
#import "loginUserViewController.h"


@interface Home : UIViewController

@property (weak, nonatomic) IBOutlet NSString *getEmail;
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *Nav;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *logout;


-(IBAction)logout:(id)sender;

-(IBAction)bandHome:(id)sender;

@end

vc2.m

import "Home.h"
#import "loginUserViewController.h"


@interface Home ()

@end

@implementation Home
@synthesize getEmail,username, band1, band2, band3;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){

    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
   // loginUserViewController *object = [[loginUserViewController alloc]init];
 //   NSString *string = [[NSString alloc] initWithFormat:@"%@", object.email.text];

    username.text = getEmail;
}

vc1.m

#import "loginUserViewController.h"
#import "Home.h"
#import "createBandViewController.h"

@interface loginUserViewController ()


@end

@implementation loginUserViewController

@synthesize email, password;
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}
- (void)viewDidload
{
    [super viewDidLoad];

}


-(IBAction)loginUser:(id)sender {
    if ([email.text isEqualToString:@""] || [password.text isEqualToString:@""])

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return;

    }


    NSMutableString *strURL = [[NSMutableString alloc] initWithFormat:@"Pretend reference/login2.php?email=%@&password=%@", email.text, password.text ];

    [strURL setString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    NSMutableString *strResult = [[NSMutableString alloc] initWithData:dataURL  encoding:NSUTF8StringEncoding];

   NSLog(@"logging in");

    if ([strResult isEqualToString:@"1"])
    {
        NSLog(@"Logged in!");
        [self performSegueWithIdentifier:@"login" sender:self];
        Home *VC;

        VC = [self.storyboard instantiateViewControllerWithIdentifier:@"Home"];
        [self.navigationController pushViewController:VC animated:YES];
        VC.getEmail = self.email.text;
    }else
    {
        // invalid information
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Invalide Information" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return;

    }
    //email.text = @"";
    password.text = @"";

}
-(void)WillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

// Dismisses keyboard by clicking out and hitting return key.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *) textField {
    [textField resignFirstResponder];
    return YES;
}
@end
TShook
  • 3
  • 2
  • Check my answer here http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/25612143#25612143 – Boda Feb 28 '16 at 19:04

1 Answers1

-1

vc2.h

@property (weak, nonatomic) IBOutlet NSString *getEmail;

vc2.m

    @synthesize getEmail;
    -(void)viewDidLoad
    {
        [super viewDidLoad];
        username.text = getEmail;
    }

vc1.m

    -(IBAction)loginUser:(id)sender {


        if ([email.text isEqualToString:@""] || [password.text isEqualToString:@""])

            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                return;
            }
       else
           {
                vc2 *VC;

                VC = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2Identifier"];
               [self.navigationController pushViewController:VC animated:YES];
                VC.getEmail = self.email.text
           }

    }

You don't need to alloc and init the view controller where you have to get the data , but you have to pass the data from vc1 to vc2 and use the variable of vc2 in vc1 , And what your were doing was using the vc1 value in vc2 . And you were allocating the LoginViewController , and in viewDidUnload method , you have set email textfield as nil . that is why you were getting nil value in vc2

Vatsal Raval
  • 311
  • 1
  • 9
  • Don't just post code. Explain what was wrong and explain how your answer solves the issue. – rmaddy Feb 28 '16 at 17:49
  • You don't need to alloc and init the view controller where you have to get the data , but you have to pass the data from vc1 to vc2 and use the variable of vc2 in vc1 , And what your were doing was using the vc1 value in vc2 . And you were allocating the LoginViewController , and in viewDidUnload method , you have set email textfield as nil . that is why yu were getting nil value in vc2 – Vatsal Raval Feb 28 '16 at 17:52
  • Update your answer with the details, don't put the details in a comment. – rmaddy Feb 28 '16 at 17:52
  • After you understand my point , add point to my answer as well as comment – Vatsal Raval Feb 28 '16 at 17:52
  • It indeed did fix the error thanks! but now it displays blank instead of nil. – TShook Feb 28 '16 at 19:53