//
// LanguageListController.h
// MyPresidents2
//
// Created by Wunderman on 12-1-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DetailViewController.h"

@interface LanguageListController : UITableViewController {
DetailViewController *detailViewController;
NSArray *languageNames;
NSArray *languageCodes;

}

@property(nonatomic, assign) DetailViewController *detailViewController;
@property(nonatomic, retain) NSArray *languageNames;
@property(nonatomic, retain) NSArray *languageCodes;



@end
//
// LanguageListController.m
// MyPresidents2
//
// Created by Wunderman on 12-1-30.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LanguageListController.h"

@implementation LanguageListController

@synthesize detailViewController = _detailViewController;
@synthesize languageNames = _languageNames;
@synthesize languageCodes = _languageCodes;

- (void)viewDidLoad {
self.languageNames = [NSArray arrayWithObjects:@"Englist", @"French", @"German", @"Spanish", nil];
self.languageCodes = [NSArray arrayWithObjects:@"en", @"fr", @"de", @"es", nil];

self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, [self.languageCodes count] * 44.0);

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.languageCodes count];
}

- (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 = [self.languageNames objectAtIndex:[indexPath row]];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.detailViewController.languageString = [self.languageCodes objectAtIndex: indexPath.row];

}


- (void)viewDidUnload {
self.detailViewController = nil;
self.languageNames = nil;
self.languageCodes = nil;
}

- (void)dealloc {
[self.detailViewController release];
[self.languageNames release];
[self.languageCodes release];
[super dealloc];
}

@end


- (IBAction)touchLanguageButton:(id)sender {

if (self.languagePopoverController.popoverVisible == NO) {

LanguageListController *languageListController = [[LanguageListController alloc]
init];
languageListController.detailViewController = self;
UIPopoverController *poc = [[UIPopoverController alloc]
initWithContentViewController:languageListController];
[poc presentPopoverFromBarButtonItem:self.languageButton
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
self.languagePopoverController = poc;
//[poc release]; 如果release ,点击四次后就抛错误。Current language: auto; currently objective-c
[languageListController release];

} else {
[self.languagePopoverController dismissPopoverAnimated:YES];
}
}