Iphone SDK - adding UITableView to UIView
- by Shashi
Hi,
I am trying to learn how to use different views, for this sample test app, i have a login page, upon successful logon, the user is redirected to a table view and then upon selection of an item in the table view, the user is directed to a third page showing details of the item.
the first page works just fine, but the problem occurs when i go to the second page, the table shown doesn't have title and i cannot add title or toolbar or anything other than the content of the tables themselves. and when i click on the item, needless to say nothing happens. no errors as well.
i am fairly new to programming and have always worked on Java but never on C(although i have some basic knowledge of C) and Objective C is new to me.
Here is the code.
import
@interface NavigationTestAppDelegate : NSObject {
UIWindow *window;
UIViewController *viewController;
IBOutlet UITextField *username;
IBOutlet UITextField *password;
IBOutlet UILabel *loginError;
//UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIViewController *viewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITextField *username;
@property (nonatomic, retain) IBOutlet UITextField *password;
@property (nonatomic, retain) IBOutlet UILabel *loginError;
-(IBAction) login;
-(IBAction) hideKeyboard: (id) sender;
@end
import "NavigationTestAppDelegate.h"
import "RootViewController.h"
@implementation NavigationTestAppDelegate
@synthesize window;
@synthesize viewController;
@synthesize username;
@synthesize password;
@synthesize loginError;
pragma mark -
pragma mark Application lifecycle
-(IBAction) hideKeyboard: (id) sender{
[sender resignFirstResponder];
}
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
//RootViewController *rootViewController = [[RootViewController alloc] init];
//[window addSubview:[navigationController view]];
[window addSubview:[viewController view]];
[window makeKeyAndVisible];
return YES;
}
-(IBAction) login {
RootViewController *rootViewController = [[RootViewController alloc] init];
//NSString *user = [[NSString alloc] username.
if([username.text isEqualToString:@"test"]&&[password.text
isEqualToString:@"test"]){
[window addSubview:[rootViewController view]];
//[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
//rootViewController.awakeFromNib;
}
else {
loginError.text = @"LOGIN ERROR";
[window addSubview:[viewController view]];
[window makeKeyAndVisible];
}
}
(void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
pragma mark -
pragma mark Memory management
(void)dealloc {
//[navigationController release];
[viewController release];
[window release];
[super dealloc];
}
@end
import
@interface RootViewController : UITableViewController {
IBOutlet NSMutableArray *views;
}
@property (nonatomic, retain) IBOutlet NSMutableArray * views;
@end
//
// RootViewController.m
// NavigationTest
//
// Created by guest on 4/23/10.
// Copyright MyCompanyName 2010. All rights reserved.
//
import "RootViewController.h"
import "OpportunityOne.h"
@implementation RootViewController
@synthesize views;
//@synthesize navigationViewController;
pragma mark -
pragma mark View lifecycle
(void)viewDidLoad {
views = [ [NSMutableArray alloc] init];
OpportunityOne *opportunityOneController;
for (int i=1; i<=20; i++) {
opportunityOneController = [[OpportunityOne alloc] init];
opportunityOneController.title = [[NSString alloc] initWithFormat:@"Opportunity %i",i];
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[[NSString alloc] initWithFormat:@"Opportunity %i",i], @ "title",
opportunityOneController, @"controller",
nil]];
self.title=@"GPS";
}
/*UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
*/
//self.title =@"Global Platform for Sales";
[super viewDidLoad];
//[temporaryBarButtonItem release];
//[opportunityOneController release];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
pragma mark -
pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [views count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [[views objectAtIndex:indexPath.row] objectForKey:@"title"];
return cell;
}
pragma mark -
pragma mark Table view delegate
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:@"controller"];
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:@"controller"];
[[self navigationController] pushViewController:targetViewController animated:YES];
}
pragma mark -
pragma mark Memory management
(void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
(void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
(void)dealloc {
[views release];
[super dealloc];
}
@end
Wow, i was finding it real hard to post the code. i apologize for the bad formatting, but i just couldn't get past the formatting rules for this text editor.
Thanks,
Shashi