views:

578

answers:

1

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.

-(IBAction) login {

RootViewController *rootViewController = [[RootViewController alloc] init];

if([username.text isEqualToString:@"test"]&&[password.text isEqualToString:@"test"]){

  [window addSubview:[rootViewController view]];

  [window makeKeyAndVisible];

}

else { loginError.text = @"LOGIN ERROR"; [window addSubview:[viewController view]]; [window makeKeyAndVisible]; } }

@interface RootViewController : UITableViewController {

IBOutlet NSMutableArray *views;

}

@property (nonatomic, retain) IBOutlet NSMutableArray * views;

  • (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";
}

[super viewDidLoad];

}

  • (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;

}

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:@"controller"];

[[self navigationController] pushViewController:targetViewController animated:YES];

}

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

A: 

Start with a navigation based application. Make as many view controllers as you needed(3 from above). Push the view controllers from one to other as you needed.

Anil Sivadas
i was trying to learn using the window based application, i can add any number of plain views with simple buttons, no issues with that, but only when i am trying to create a UIView->UITableView->UIView i am having problems. in the second view all i see is the table, no title, no toolbar nothing. i could create a navigation based application where i had a UITableView - > UIView, which worked just fine.
Shashi