2011-03-19 82 views
1

可以幫助澄清如何創建UIPickerView一旦按鈕被點擊?請給予幫助......非常感謝!點擊按鈕後如何創建UIPickerView?

其實,如下所示是代碼,我做什麼,一旦我點擊了按鈕選擇器視圖中顯示,但沒有任何數據,請找到下面的代碼: -

-(IBAction)buttonPressed1:(id)sender{ 

    CGSize pickerSize = [myLocationPickerView sizeThatFits:CGSizeZero]; 
    myLocationPickerView.frame = CGRectMake(0.0,250,pickerSize.width,pickerSize.height); 

    [self.view addSubview:myLocationPickerView];} 

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    arrayNo = [[NSMutableArray alloc] init]; 
    [arrayNo addObject:@" example 1 "]; 
    [arrayNo addObject:@" example 2 "]; 
    [arrayNo addObject:@" example 3 "]; 
    [arrayNo addObject:@" example 4 "]; 
    [myLocationPickerView selectRow:0 inComponent:0 animated:NO]; 

} 



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView; 
{ 
    return 1; 
} 


- (void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 
{ 
    mlabel.text= [arrayNo objectAtIndex:row]; 
} 



- (NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView numberOfRowsInComponent:(NSInteger)component; 
{ 
    return [arrayNo count]; 
} 



- (NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    return [arrayNo objectAtIndex:row]; 

} 

回答

-1

有是一對夫婦的方式來做到這一點,這取決於你的情況。最直接的方法是在界面構建器中創建一個xib,並在單擊該按鈕時推送新視圖。

另一種方式是以編程方式組裝視圖,然後添加pickerview。你真的需要澄清什麼按鈕被點擊,你有困難的地方以及你已經可以做多少。

編輯:我忘了我最常用的方法。以模態呈現xib:

// push modal controller 
EditViewController *editController = [[EditViewController alloc] initWithNibName:@"EditView" 
                      bundle:nil]; 
editController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
editController.delegate = self; 

editController.selectedSyllableNumber = tempSyllable.nameNumber; 
editController.selectedDirectionNumber = tempSyllable.directionNumber; 
editController.sequenceType = tempSyllable.exercise.typeNumber; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editController]; 
[self presentModalViewController:navController 
         animated:YES];