2013-08-14 4 views
1

단일 뷰 응용 프로그램의 템플렛으로 응용 프로그램을 만들었습니다. 그런 다음 레이블을 추가하고이를 내 ViewController .h 파일에 연결했습니다. 그런 다음 피커를 만들고 그것을 채운 다음 텍스트 필드에 도구 모음을 설정합니다. 그러나 필자가 텍스트 필드를 탭하면 피커는 모두 검정색입니다. 이것이 의미가 없으면, 코드는 그것을 전부 설명 할 것입니다.텍스트 필드를 두 드릴 때 UIPicker 팝업이 나타납니다

.H

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
} 

@property (weak, nonatomic) IBOutlet UITextField *habitField; 

@property (weak, nonatomic) NSArray *PickerData; 

@end 

하는 .m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil]; 
    self.PickerData = array; 

    UIToolbar *toolBar = [[UIToolbar alloc] init]; 
    toolBar.barStyle = UIBarStyleBlackOpaque; 
    [toolBar sizeToFit]; 

    [toolBar setBackgroundImage:[UIImage imageNamed:@"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                      target:self 
                      action:nil]; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                      target:self 
                      action:@selector(releasePicker)]; 

    UIPickerView *Picker = [[UIPickerView alloc] init]; 

    doneButton.image = [UIImage imageNamed:@"button.png"]; 

    [toolBar setItems:@[flexSpace, doneButton] animated:YES]; 
    self.habitField.inputAccessoryView = toolBar; 

    [self.habitField setInputView:Picker]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    return [self.PickerData count]; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [self.PickerData objectAtIndex:row]; 
} 

@end 

시뮬레이터는 당신이 선택 도구에 대한 (UIPickerViewDelegate을) 위임을 설정하는 것을 잊었다 보듯이

enter image description here

답변

1

처럼 보이는

 Picker.delegate = self; 

추가해야합니다 :)

@interface ViewController : UIViewController<UIPickerViewDelegate> { 

을}

건배를