也许这是一个愚蠢的问题但是我在子视图中切换视图时遇到了问题。让我解释一下代码结构:
我有这些类文件:
/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中,我已成功插入子视图“Login”,并在应用程序启动时显示。
在登录视图中,输入我的用户名和密码,然后单击“登录”按钮。单击此按钮时,将触发IBAction。在这个IBAction中,我想用Customers更改当前的子视图。
这是我用过的代码:
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.view removeFromSuperview];
[tmpViewController release];
}
@end
如上所示,在LoginController.m的loginToAccount方法中,我正在检查登录信息,然后为“customers”子视图设置新的视图控制器。
然后我从超级视图中删除当前的“Login”子视图,但不知道如何添加新的“Customers”子视图。
在MainWindow.xib中,我有一个链接到ViewController类的视图控制器,它是根控制器。
感谢任何帮助。因为我是Objective-C和iPhone编程的新手,所以请尽量解释一下新手程序员的问题:)
再次感谢。
答案 0 :(得分:2)
您没有向视图层次结构添加任何视图,只是删除了登录视图控制器。如果要将客户视图添加到视图层次结构中,则应使用:
CustomersController *tmpViewController = [[CustomersController alloc] initWithNibName:@"Customers" bundle:nil];
self.customersController = tmpViewController;
[self presentModalViewController:tmpViewController]
上述方法将确保在帐户视图控制器上调用viewWillLoad,didLoad等。它还将为您删除和添加正确的视图到视图层次结构。