Question

Im trying to add the UiPickerView class from https://github.com/nicklockwood/CountryPicker I want to use with storyboard in my viewController but the delegate method is never getting called although the pickerView shows the countries. Here is what ive done: ive imported the CountryPicker folder and Flags folder, in the storyboard controller ive added a UiPickerView and assign it to CountryPicker class. The ViewController.h and .m files are exactly the same as the example. i cant find what im missing. Also i tried init the object and call the setSelectedCountry method from my ViewController.m file but its also not doing anything. ViewController.h

#import <UIKit/UIKit.h>
#import "CountryPicker.h"

@interface ViewController : UIViewController <CountryPickerDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
 [super viewDidLoad];
 CountryPicker *picker = [[CountryPicker alloc] init];
 [picker.delegate self];
 [picker setSelectedCountryCode:@"US" animated:YES];
}

- (void)countryPicker:(__unused CountryPicker *)picker didSelectCountryWithName:(NSString *)name code:(NSString *)code
{
 NSLog(@"country: %@",name);
}


@end
Was it helpful?

Solution

You need to create an IBOutlet from the storyboard to your code Apple Docs

Remove the code you have in your viewDidLoad that creates a new picker.

Then for the the new IBOutlet you've created, set self as the delegate, so if your outlet was called picker your viewDidLoad should look as follows:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.picker.delegate = self;
    [self.picker setSelectedCountryCode:@"US" animated:YES];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top