2011-07-04 59 views
2

IPHONE的問題...我嘗試了幾個不同的模板和其他人的幫助,但我似乎無法得到這個工作,它不應該太難了。我想添加一個帶有「3組件」的UIPickerView,每個組件都是數字值(例如,它們可以在50-100之間選擇),並顯示它們在msg窗口中選擇的內容。 (計算部分我會後來弄清楚,但首先我需要讓組件正確註冊第三列,遇到問題後出現問題....任何想法?如何創建一個3組件UIPickerView(模板任何人???)iPhone

*我不是最有經驗的,並聽說這是一個很好的地方,可以從一些非常聰明的人那裏得到答案,我真的很感謝任何人提供的幫助或任何人提供的「模板」大綱,以供任何人使用3個組件的UIPickerView ...感謝高級>>> Lawrence

這是我的ViewController.m代碼:

#import "DoubleComponentPickerViewController.h" 

@implementation DoubleComponentPickerViewController 

@synthesize doublePicker; 
@synthesize oneTypes; 
@synthesize twoTypes; 
@synthesize threeTypes; 


// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 


-(IBAction)buttonPressed 
{ 
    NSInteger oneRow = [doublePicker selectedRowInComponent: 
          kOneComponent]; 
    NSInteger twoRow = [doublePicker selectedRowInComponent: 
          kTwoComponent]; 
    NSInteger threeRow = [doublePicker selectedRowInComponent: 
          kThreeComponent]; 
    NSString *one = [oneTypes objectAtIndex:oneRow]; 
    NSString *two = [twoTypes objectAtIndex:twoRow]; 
    NSString *three = [threeTypes objectAtIndex:threeRow]; 

    NSString *message = [[NSString alloc] initWithFormat: 
    @"First = %@   Second = %@   Third = %@",one, two, three]; 

    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Please Confirm Numbers" message:message delegate:nil cancelButtonTitle:@"Confirm" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [message release]; 
} 


/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 
} 
*/ 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    NSArray *oneArray = [[NSArray alloc] initWithObjects: 
    @"91",@"92",@"93",@"94",@"95",@"96",nil]; 
    self.oneTypes = oneArray; 
    [oneArray release]; 

    NSArray *twoArray = [[NSArray alloc] initWithObjects: 
    @"97",@"98",@"99",@"100",@"101",@"102",@"103", nil]; 
    self.twoTypes = twoArray; 
    [twoArray release]; 

    NSArray *threeArray = [[NSArray alloc] initWithObjects: 
    @"102",@"103",@"104",@"105",@"106",@"107", nil]; 
    self.threeTypes = threeArray; 
    [threeArray release]; 
    [super viewDidLoad]; 
} 



// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [doublePicker release]; 
    [oneTypes release]; 
    [twoTypes release]; 
    [threeTypes release]; 
    [super dealloc]; 
} 


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
    { 
    return 3; 
    } 

-(NSInteger)pickerView:(UIPickerView *)pickerView 
    numberOfRowsInComponent:(NSInteger)component 
    { 
     if (component == kOneComponent) 
     return[self.oneTypes count]; 
     return[self.twoTypes count]; 
     return[self.threeTypes count]; 

     if (component == kTwoComponent) 
     return[self.oneTypes count]; 
     return[self.twoTypes count]; 
     return[self.threeTypes count]; 

     if (component == kThreeComponent) 
     return[self.oneTypes count]; 
     return[self.twoTypes count]; 
     return[self.threeTypes count]; 
    } 

-(NSString *)pickerView:(UIPickerView *)pickerView 
    titleForRow:(NSInteger)row 
    forComponent:(NSInteger)component 
    { 
    if (component == kThreeComponent) 
    return [self. threeTypes objectAtIndex:row]; 
    return [self. twoTypes objectAtIndex:row]; 
    return [self. oneTypes objectAtIndex:row]; 

    } 



@end 
+0

的文檔是你的朋友。學會傾聽你的朋友。 – dasdom

回答

2

您可以實現數據源方法ŝ

// returns the number of 'columns' to display. 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 

// returns the # of rows in each component.. 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 

,然後你可以實現的委託方法

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
+0

我不確定在哪添加,或添加什麼,如果我添加?任何建議...這是我的viewController.m文件現在 – Lawrence

+0

是的,你可以寫下pickerView.delegate = self;和pickerView.datasource = self;並在同一控制器中實現上述方法。 – iMOBDEV

+0

感謝您的快速回復我會試試這個......會很高興有人與您聊天並定期提問像您這樣的幫助,真的好像知道如何做事... – Lawrence

相關問題