views:

351

answers:

2

Hi Everyone,

I have three view controllers, one root controller, one login view controller and one customers view controller. I want to pass the entered username and password in login view controller to the customers view controller. My files and code is displayed below, could you please guide me, how can access to variables set in the login view controller? Or how can I pass variables to customers view controller?

I have these class files:

/classes/MySoftwareAppDelegate.h
/classes/MySoftwareAppDelegate.m
/classes/ViewController.h
/classes/ViewController.m
/classes/LoginController.h
/classes/LoginController.m
/classes/CustomersController.h
/classes/CustomersController.m

I have these views:

/resources/MainWindow.xib
/resources/Login.xib
/resources/Customers.xib

In the AppDelegate, I have successfully inserted the sub view "Login" and it's displayed whenever the app starts.

In the login view, I enter my username and password and then click the "Login" button. When this button is clicked, an IBAction is triggered. In this IBAction, I want to change the current subview with the Customers.

Here's the code I have used:

MySoftwareAppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface MySoftwareAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
 ViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewController *viewController;

@end

MySoftwareAppDelegate.m

#import "MySoftwareAppDelegate.h"
#import "ViewController.h"

@implementation MySoftwareAppDelegate

@synthesize window;
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
 [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
 [viewController release];
    [window release];
    [super dealloc];
}


@end

ViewController.h

#import <UIKit/UIKit.h>

@class LoginController;

@interface ViewController : UIViewController {
 LoginController *loginController;
}

@property (nonatomic, retain) LoginController *loginController;

@end

ViewController.m

#import "ViewController.h"
#import "LoginController.h"

@implementation ViewController

@synthesize loginController;

- (void)viewDidLoad {
 LoginController *tmpViewController = [[LoginController alloc] initWithNibName:@"Login" bundle:nil];

 self.loginController = tmpViewController;
 [self.view insertSubview:loginController.view atIndex:0];

 [tmpViewController release];

    [super viewDidLoad];
}

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

 if (self.loginController.view.superview == nil) {
  self.loginController = nil;
 }

 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
 [loginController release];
    [super dealloc];
}

@end

LoginController.h

#import <UIKit/UIKit.h>

@class CustomersController;

@interface LoginController : UIViewController {
 UIButton *loginButton;
 UITextField *usernameTextField;
 UITextField *passwordTextField;
 NSMutableString *available_credits;
 NSString *current_xml_element;
 CustomersController *customersController;
}

@property (nonatomic, retain) IBOutlet UIButton *loginButton;
@property (nonatomic, retain) IBOutlet UITextField *usernameTextField;
@property (nonatomic, retain) IBOutlet UITextField *passwordTextField;
@property (nonatomic, retain) NSMutableString *available_credits;
@property (nonatomic, retain) NSString *current_xml_element;
@property (nonatomic, retain) CustomersController *customersController;

-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)loginToAccount:(id)sender;

@end

LoginController.m

#import "LoginController.h"
#import "CustomersController.h"

@implementation LoginController

@synthesize loginButton;
@synthesize usernameTextField;
@synthesize passwordTextField;
@synthesize customersController;

- (void)viewDidLoad {
 UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
 UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
 UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
 UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

 [loginButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
 [loginButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning]; 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}

- (void)dealloc {
 [usernameTextField release];
 [passwordTextField release];
    [super dealloc];
}

-(IBAction)textFieldDoneEditing:(id)sender {
 [sender resignFirstResponder];
}

-(IBAction)backgroundTap:(id)sender {
 [usernameTextField resignFirstResponder];
 [passwordTextField resignFirstResponder];
}

-(IBAction)loginToAccount:(id)sender {

 // bla bla bla... Login check process is done here

 CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:@"Customers" bundle:nil];
 self.customersController = tmpViewController;

 [self presentModalViewController:tmpViewController animated:YES];
 [self.view removeFromSuperview];

 [tmpViewController release];

}

@end

As you can see above, in LoginController.m's loginToAccount method, I am checking the login info and then setting the new view controller for the "customers" sub-view.

Then I am removing the current "Login" subview from the super view but don't know how to add the new "Customers" sub view.

In MainWindow.xib, I have one view controller which is linked to ViewController class and it's the root contoller.

Any help is appreciated. Because I am new to Objective-C and iPhone programming, please do your best to explain considering a novice programmer :)

Thanks again.

+1  A: 

Okay, let me answer my question. I just found the answer on StackOverFlow.com

In the view controller which is going to load the next view controller, just add these lines:

NextController *tmpViewController = [[NextController alloc] initWithNibName:@"NextView" bundle:nil];
tmpViewController.enteredUsername = usernameTextField.text;
tmpViewController.enteredPassword = passwordTextField.text;
TamTam
+2  A: 

I'd say that better way is to have separate class for storing globally needed data (and that would be compliant with MVC model). For example you can store you login information in your MySoftwareAppDelegate, which is easily accessible with [[UIApplication sharedApplication] delegate] call from any part of your application.

Vladimir
Thanks, I will follow that way.
TamTam