带有CountryPicker类的ios UiPickerView

时间:2014-03-08 10:38:26

标签: ios uipickerview

我试图从https://github.com/nicklockwood/CountryPicker添加UiPickerView类 我想在我的viewController中使用storyboard但是,虽然pickerView显示了这些国家,但委托方法永远不会被调用。 这就是我所做的: 我已经在故事板控制器中导入了CountryPicker文件夹和Flags文件夹,我添加了一个UiPickerView并将其分配给CountryPicker类。 ViewController.h和.m文件与示例完全相同。 我无法找到我失踪的东西。 此外,我尝试初始化对象并从我的ViewController.m文件调用setSelectedCountry方法,但它也没有做任何事情。 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

1 个答案:

答案 0 :(得分:1)

您需要从故事板创建一个IBOutlet到您的代码Apple Docs

删除viewDidLoad中创建新选择器的代码。

然后,对于您创建的新IBOutlet,请将self设置为代理人,因此,如果您的商店picker被调用,则viewDidLoad应如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.picker.delegate = self;
    [self.picker setSelectedCountryCode:@"US" animated:YES];
}
相关问题