开发者

connect a UINavigation Controller to table view within view based app

I have an view based app that has a main menu. In the main menu the user can select an option that presents a table that is populated from an SQLite database (BrowseView). That is all working fine, however from the table I want the user to be able to select a row of data that will present a detailed view and I cannot get the detailed view to work. Nothing happens.

I think the problem is that I havn't connected the detailed view to the UINavigation controller but I'm unsure on how to do this as I have tried everything that I can think of (which is probably not much as I'm new to programming).

Any help on this would be greatly appreicated. Here is my code.

In the appDelegate.h

@class ClubFindViewController;

@interface ClubFindAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ClubFindViewController *viewController;
UINavigationController *navigationController;

NSMutableArray *clubArray;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ClubFindViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) NSMutableArray *clubArray;

-(void) copyDatadaseIfNeeded;
-(NSString *) getDBPath;


@end

AppDelegate.m

@implementation ClubFindAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize navigationController;
@synthesize clubArray;



#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

[self copyDatadaseIfNeeded];
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
self.clubArray = tempArray;
[tempArray release];

[Clubs getInitialDataToDisplay:[self getDBPath]];

self.window.rootViewController = self.navigationController;

[self.window addSubview:navigationControll开发者_如何学Goer.view];
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

return YES;
}

DetailView.h

@class Clubs;


@interface DetailViewController : UITableViewController {
IBOutlet UITableView *tableView;
Clubs *clubObj;
}

@property (nonatomic, retain) Clubs *clubObj;
@property (nonatomic, retain) IBOutlet UITableView *tableView;


@end

DetailView.m

#import "DetailViewController.h"
#import "Clubs.h"
#import "BrowseViewController.h"


@implementation DetailViewController

@synthesize clubObj;
@synthesize tableView;


-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = clubObj.clubName;
[self.tableView reloadData];

}



-(NSString *)tableView:(UITableView *)tblView titleForHeaderInSection: (NSInteger)section {
NSString *sectionName = nil;
switch (section) {
    case 0:
        sectionName = [NSString stringWithFormat:@"Club"];
        break;
    case 1:
        sectionName = [NSString stringWithFormat:@"Address"];
        break;
}
return sectionName;

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
// Return the number of rows in the section.
return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath   *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

switch (indexPath.section) {
    case 0:
        cell.textLabel.text = clubObj.clubName;
        break;
    case 1:
        cell.textLabel.text = clubObj.ClubAddress;
        break;
}

return cell;


}

Updated code.


When you create your app and want to use UINavigationController you must in your AppDelegate tell that UINavigationController is your rootViewController.

self.window.rootViewController = self.navigationController;

When the Detail view will be pushed it will already (know) that it is inside the UINavigationController and so you don't need to create additional UINavigationController.

So delete UINavigationController *navigationController; from both .h and .m file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜