1

I'm a xcode noob, but here's what I'm looking for followed by what I've done. I've created a login and with that login usernameField I'm trying verify the user and open a only a web page specific to that person. The login is working here's all my code

//LoginViewController.h


    #import <UIKit/UIKit.h>
    @interface LoginViewController : UIViewController
    {
    IBOutlet UITextField *usernameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UIButton *loginButton;   
    IBOutlet UIActivityIndicatorView *loginIndicator;
    }

    @property(nonatomic, strong) UITextField *usernameField;
    @property(nonatomic, strong) UITextField *passwordField;
    @property(nonatomic, strong) UIButton    *loginButton;
    @property(nonatomic, strong) UIActivityIndicatorView *loginIndicator;

    -(IBAction) login: (id) sender;
    @end


//LoginViewController.m


    #import "LoginViewController.h"
    @implementation LoginViewController
    @synthesize usernameField, passwordField, loginButton, loginIndicator;

    -(IBAction) login: (id) sender
    {
    if ([usernameField.text isEqualToString: @"userOne"] && [passwordField.text
    isEqualToString: @"passwordOne"])
    {
    printf("Success")
    //here is where I would like to pass usernameField.text to WebViews
    }
    else if (([usernameField.text isEqualToString: @"userTwo"] && [passwordField.text 
    isEqualToString: @"passwordTwo"])
    {
    printf("Success")
    //here is where I would like to pass usernameField.text to WebViews
    }
    else
    {
    printf("Login Failed")
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed"
                 message:@"Wrong username and password" delegate:self
                 cancelButtonTitle:@"Done"
                 otherButtonTitles:nil];
    [alert show];
    }
    loginIndicator.hidden=False;
    [loginIndicator startAnimating];
    }
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if([[segue identifier] isEqualToString:@"fromLogin"])
    {
        WebViews *wvc = [segue destinationViewController];
        wvc.usernamerField = self.usernameField.text;
    }
    }
    @end

//WebViews.h

    @interface WebViews : UIViewController
    {
    IBOutlet UIWebView *webView;
    NSString *usernameField;
    }
    @property (nonatomic, strong) NSString*usernameField;

    @end

//WebViews.m

 #import "WebViews.h"
    @interface WebViews ()//To be honest I'm not sure what this is for
    @end
    @implementation WebViews
    @synthesize usernameField;

    -(void)viewDidLoad
    {
    [super viewDidLoad];
    NSLog(@"username is = %@", self.usernameField);
    if([T.usernameField.text isEqualToString: @"userOne"])
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL 
    URLWithString:@"http://www.google.com"]]];
    }
    else if([T.usernameField.text isEqualToString: @"userTwo"])
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.yahoo.com"]]];
    }
    else
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.wikipedia.com"]]];
    }

    @end

Any help is greatly appreciated

1 Answers1

0

Your problem is that you are never assigning anything to T so it is nil in your method.

In the code for your LoginViewController you need to pass the username to the WebViewController. You can do this by implementing the prepareForSegue method.

Here is an example, taken from How to pass prepareForSegue: an object

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        WebViewController *wvc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        wvc.username = self.usernameField.text;
    }
}
Community
  • 1
  • 1
Nathan Villaescusa
  • 17,331
  • 4
  • 53
  • 56
  • Try logging when each function is getting called, and look at when wvc.username is getting assigned. Things are probably happening out of order. – Nathan Villaescusa Oct 18 '12 at 22:39
  • You don't want any communication between the two views. When the second appears you want the first to be garbage collected. – Nathan Villaescusa Oct 22 '12 at 23:52
  • Hey Nathan, I just wanted to thank you for all your help. This code does work, my problem was is that I was trying to have the login at the beginning of my program and then later access the information, now I'm not sure if I can have two segues but that's essentially what I'd need unless you know of away to access the information later in the program? – user1754014 Oct 29 '12 at 23:33
  • You should either store the username someplace global, or you could create a model object that you can pass from view to view. I'd probably chose the second route. – Nathan Villaescusa Oct 29 '12 at 23:34
  • So I've created Users.h and Users.m; Users.h is simply `@interface Users: NSObject {NSString *username;} @property (nonatomic, retain) NSString *username; @end` Then in LoginViewController i do `Users*user = [[Users alloc] init]; user.username =@"userOne"` Now I believe I've created the object properly but is it global? – user1754014 Nov 01 '12 at 17:09
  • It is not. You would need to create a Users singleton. http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like – Nathan Villaescusa Nov 01 '12 at 17:22