2009-10-15 164 views
0

我有三個視圖控制器,一個根控制器,一個登錄視圖控制器和一個客戶視圖控制器。我想將登錄視圖控制器中輸入的用戶名和密碼傳遞給客戶視圖控制器。我的文件和代碼顯示在下面,請你指導我,如何訪問登錄視圖控制器中設置的變量?或者我怎樣才能將變量傳遞給客戶視圖控制器?如何將變量從一個視圖控制器傳遞給另一個視圖控制器?

我有這些類文件:

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

我有這方面的觀點:

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

在AppDelegate中,我已經成功地插入副視點「登錄」,並將其顯示在應用程序啓動時。

在登錄視圖中,輸入我的用戶名和密碼,然後單擊「登錄」按鈕。點擊此按鈕時,會觸發IBAction。在這個IBAction中,我想要改變與客戶的當前子視圖。

下面是我用的代碼:

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 

正如您在上面看到的,在LoginController.m的loginToAccount方法中,我正在檢查登錄信息,然後爲「customers」子視圖設置新的視圖控制器。

然後,我從超級視圖中刪除當前的「登錄」子視圖,但不知道如何添加新的「客戶」子視圖。

在MainWindow.xib中,我有一個視圖控制器鏈接到ViewController類,它是根控制器。

任何幫助表示讚賞。因爲我是Objective-C和iPhone編程的新手,請盡力解釋考慮一位新手程序員:)

再次感謝。

回答

2

我會說,更好的辦法是有單獨的類用於存儲全球需要的數據(和這將符合MVC模型)。 例如,您可以將登錄信息存儲在MySoftwareAppDelegate中,通過您的應用程序的任何部分的[[UIApplication sharedApplication] delegate]調用即可輕鬆訪問該信息。

+0

謝謝,我會按照這樣的方式。 – TamTam 2009-10-15 16:29:11

3

好的,讓我回答我的問題。我剛剛在StackOverFlow上找到了答案。COM

在其將要加載下一個視圖控制器視圖控制器,只需添加這些行:

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

這一切都取決於你想傳遞的數據有多嚴重。對於一個快速變量(可能是模態視圖控制器中的設置更改),TamTam的解決方案是最有意義的。你分配/初始化它,你有變量,爲什麼不訪問它的屬性?同樣的(模態表示)視圖控制器可能會通過委託模式傳回變量。

如果您的數據需要在系統範圍內,您可以使用單例模式。使用「[[UIApplication sharedApplication]委託]」獲取應用程序委託(這是一個單例),並且許多人爲了方便而在其中填入變量。但是,您的應用程序委託並不是爲此設計的,因此它被認爲是不好的形式。如果你的蘋果不是快速的,創建你自己的單身人士。

如果您使用像sql,plists或coredata這樣的持久性數據存儲,則可以將系統範圍的數據放在那裏。

相關問題