2010-04-08 85 views
0

我寫了一個UIPicker,它是從.plist填充的。這部分工作正常。 我不知道如何做的是一旦選擇的行是顯示在另一個UIView中的底層數據。UIPicker併發送選定的行數據到UIView

在我的.m文件中的代碼是:

#import "airlinePickerViewController.h" 


@implementation airlinePickerViewController 

@synthesize picker; 
@synthesize airlines; 
@synthesize airline; 
@synthesize teleno; 



- (IBAction)butonPressed:(id)sender 
{ 
NSInteger airRow = [picker selectedRowInComponent:kAirlineComponent]; 
NSInteger telRow = [picker selectedRowInComponent:kTelenoComponent]; 

NSString *air = [self.airline objectAtIndex:airRow]; 
NSString *tel = [self.teleno objectAtIndex:telRow]; 

NSString *title = [[NSString alloc] initWithFormat:@"You selected %@.", tel]; 
NSString *message = [[NSString alloc] initWithFormat:@"%@ is in %@", tel, air]; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert show]; 
[alert release]; 
[title release]; 
[message release]; 
} 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
    // Initialization code 
} 
return self; 
} 

- (void)viewDidLoad { 

NSBundle *bundle = [NSBundle mainBundle]; 
NSString *plistPath = [bundle pathForResource:@"airlinedictionary" ofType:@"plist"]; 
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 
self.airlines = dictionary; 
[dictionary release]; 

NSArray *components = [self.airlines allKeys]; 
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)]; 
self.airline = sorted; 

NSString *selectedAirline = [self.airline objectAtIndex:0]; 
NSArray *array = [airlines objectForKey:selectedAirline]; 
self.teleno = array; 
} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
// Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
[picker release]; 
[airlines release]; 
[airline release]; 
[teleno release]; 
[super dealloc]; 
} 
#pragma mark - 
#pragma mark Picker Data Source Methods 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
return 1; 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
if (component == kAirlineComponent) 
    return [self.airline count]; 
return [self.teleno count]; 
} 
#pragma mark Picker Delegate Methods 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
if (component == kAirlineComponent) 
    return [self.airline objectAtIndex:row]; 
return [self.teleno objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
if (component == kAirlineComponent) 
{ 
    NSString *selectedAirline = [self.airline objectAtIndex:row]; 
    NSArray *array = [airlines objectForKey:selectedAirline]; 
    self.teleno = array; 
    [picker selectRow:0 inComponent:kTelenoComponent animated:YES]; 
    [picker reloadComponent:kTelenoComponent]; 
} 
} 
@end 

誰能幫我把與如何完成這個任務交手。

非常感謝

德雷克

回答

0

現在不知何故籠統的回答: 我假設你想切換到一個完全新的視圖(而不是子視圖),所以第一件事情,你可能需要的是一個導航控制器或TabBarController,以方便推送/切換到新視圖。在切換/推送新視圖之前,您可以在初始化之後,但在切換到新視圖之前,將所選值作爲屬性分配給新視圖。

相關問題