Frage

I attempted to build a basic application prototype (mimic real application flow). I created one view controller class and linked the UIPickerView to the .h file. Wrote the logic but still nothing is reflecting in the picker.

Am I missing something? Here you go with the code:

@implementation SelectCountryForViewAdsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1; // For one column
}

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

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.colorArray objectAtIndex:row]; // If it's a string
}



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

    self.colorArray  = [[NSArray alloc]         initWithObjects:@"Blue",@"Green",@"Orange",@"Purple",@"Red",@"Yellow" , nil];

}

and in the .h file:

@interface SelectCountryForViewAdsViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIPickerView *countrySelector;
@property (strong, nonatomic) NSArray *colorArray;

@end

Any help is appreciated. I am a beginner in iOS devs.

War es hilfreich?

Lösung

Please check below image you need to right click on UIPicker view and connect it File owner.

enter image description here

Please check below link for more detail

http://www.techotopia.com/index.php/An_iOS_5_iPhone_UIPickerView_Example

http://iosmadesimple.blogspot.in/2012/09/uipickerview-tutorial.html (More helpful)

http://www.developersalmanac.com/uipickerview-tutorial/

Andere Tipps

[countrySelector setDelegate:self];
 [countrySelector setDataSource:self];

//Use this code in Viewdidload method

Add UIPickerViewDataSource and UIPickerViewDelegate in your interface file.

In viewDidLoad:

self.countrySelector.delegate = self;
self.countrySelector.dataSource = self;

You can set the delegate and dataSource directly from the xib file. But don't forget to mention them in the interface file.

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];
    [lblRow setTextAlignment:NSTextAlignmentCenter];
    [lblRow setTextColor: [UIColor strawberryColor]];
    [lblRow setText:self.colorArray[row]];
    [lblRow setFont:[UIFont fontWithName:@"Avenir-Medium" size:18]];
    [lblRow setBackgroundColor:[UIColor clearColor]];

    return lblRow;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top