iPhone从View转到TableView

时间:2010-02-24 19:35:07

标签: iphone uitableview

我有一个登录视图,在检查用户名后我想进入表格视图。 我从Apple示例代码下载了SimpleDrillDown应用程序,我想在运行应用程序时首先查看登录页面,然后再查看TableView。

如果有人有时间可以在这里找到项目: http://developer.apple.com/iphone/library/samplecode/SimpleDrillDown/index.html

我所做的改变是:

SimpleDrillDownAppDelegate.m

import "SimpleDrillDownAppDelegate.h"
import "RootViewController.h"
import "LoginViewController.h"
import "DataController.h"


@implementation SimpleDrillDownAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;
@synthesize loginViewController;
@synthesize dataController;


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


    //thomas add this
    LoginViewController *_loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:[NSBundle mainBundle]];
    self.loginViewController = _loginViewController;
//  [_loginViewController release];
//  
    //thomas add this

    // Create the data controller.
    //DataController *controller = [[DataController alloc] init];
//    self.dataController = controller;
//    [controller release];

    //rootViewController.dataController = dataController;

    /*
     The navigation and root view controllers are created in the main nib file.
     Configure the window with the navigation controller's view and then show it.
     */
    //thomas commented this and copy this into LoginView
    //[window addSubview:[navigationController view]];

    //thomas add this
    [window addSubview:[loginViewController view]];
    //thomas add this


    [window makeKeyAndVisible];
}


- (void)dealloc {
    //[navigationController release];
    //[rootViewController release];
    [loginViewController release];
    [window release];
    //[dataController release];
    [super dealloc];
}

@end

SimpleDrillDownAppDelegate.h

@class DataController;
@class RootViewController;
@class LoginViewController;

@interface SimpleDrillDownAppDelegate : NSObject  {

    UIWindow *window;

    UINavigationController *navigationController;
    RootViewController *rootViewController;
    LoginViewController *loginViewController;

    DataController *dataController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet LoginViewController *loginViewController;

@property (nonatomic, retain) DataController *dataController;

@end

LoginViewController.h

#import 

@class DataController;
@class RootViewController;
@class LoginViewController;

@interface LoginViewController : UIViewController {
    DataController *dataController;
    IBOutlet UITextField *usernameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UIButton *loginButton;
    IBOutlet UIActivityIndicatorView *loginIndicator;
    UINavigationController *navigationController;
    RootViewController *rootViewController;


}

@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIActivityIndicatorView *loginIndicator;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) DataController *dataController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;


- (IBAction) login: (id) sender;

@end

LoginViewController.m

#import "LoginViewController.h"
#import "DataController.h"
#import "RootViewController.h"




@implementation LoginViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize loginIndicator;
@synthesize navigationController;
@synthesize dataController;
@synthesize rootViewController;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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 {
    [super dealloc];
}

- (IBAction) login: (id) sender
{
    // TODO: spawn a login thread
    NSString *userName = usernameField.text;
    NSString *pass = passwordField.text;

    loginIndicator.hidden = FALSE;
    [loginIndicator startAnimating];

    loginButton.enabled = FALSE;

    //Hardcode here the credentials



    if ([userName isEqualToString: @"test"] && [pass isEqualToString: @"test"]){
    // Create the data controller.
    DataController *controller = [[DataController alloc] init];
    self.dataController = controller;
    [controller release];

    rootViewController.dataController = dataController;


  [self pushViewController:self.navigationController animated:YES];
    }else{
        printf("ERROR");
    }



}


@end

最后我收到此错误

错误日志

2010-02-24 21:19:55.595 SimpleDrillDown[97651:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LoginViewController pushViewController:animated:]: unrecognized selector sent to instance 0x190d8a0'
2010-02-24 21:19:55.595 SimpleDrillDown[97651:207] Stack: (
    807902715,
    2501092617,
    808284155,
    807854166,
    807706786,
    18813,
    814709201,
    815110321,
    815119058,
    815114808,
    814812979,
    814722763,
    814748641,
    839148405,
    807687520,
    807683624,
    839142449,
    839142646,
    814752238,
    9140,
    8994
)

很抱歉这篇长篇文章,但我不知道在哪里搜索我已经在这里读了一篇帖子:(

先谢谢你们所有人

3 个答案:

答案 0 :(得分:0)

在您的登录IBAction中,向LoginViewController发送pushViewController: animated:消息。该消息及其对应部分popViewControllerAnimated:在UINavigationController类中可用。

不幸的是,你的LoginViewController不是UINavigationController。

您可以通过将其更改为:

来修复该特定错误
[self.navigationController pushViewController:self.dataController animated:YES];

这假设LoginViewController由UINavigationController托管。

答案 1 :(得分:0)

焦是对的。你需要告诉一个navigationController推送视图,而不是一个viewController子类。 navigationController应该处理您应用程序的分层结构部分所需的所有视图控制器。您的navigationController应首先使用其initWithRootViewController:方法加载您的logincontroller并显示其视图。然后,当你想显示下一个视图时,你会发送一个pushViewController:animated:message的navigtionController,下一个viewController你想要显示的视图。

答案 2 :(得分:0)

另一种方法是创建第一个视图作为持有UITableView的导航控制器,然后在viewWillAppear上,检查用户是否已登录。如果他们尚未登录,请将您创建的LoginViewController呈现为模态。使用模态的好处是根视图将是您的tableview。如果您的应用程序有一个标签栏,并且您的第一个导航控制器视图是loginview控制器,则每次单击标签栏项目时,都会显示您的登录视图。

希望这有帮助。