1.
// 初始化手机号国家前缀按钮
self.countryCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(24, 5, 58, 40)];[self.countryCodeBtn addTarget:self action:@selector(selectCountryCode) forControlEvents:UIControlEventTouchUpInside];
[whiteView addSubview:self.countryCodeBtn];
2.实现selectCountryCode方法
- (void)selectCountryCode {
__weak typeof(self) weakSelf = self;
SelectCountryCodeViewController *selectCountryCodeViewController = [[SelectCountryCodeViewController alloc] init];
selectCountryCodeViewController.countryCodeSelected = ^(NSString *countryName, NSString *code) {
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf selectedCountryName:countryName andCode:code];
};
UINavigationController *nav = [self navigationControllerWithRootViewController:selectCountryCodeViewController navBarTintColor:AppBlueColor];
[self presentViewController:nav animated:YES completion:nil];
}
//countryCodeSelected //这是一个Block。。。。 要声明它
@property (nonatomic,copy) void(^countryCodeSelected)(NSString *countryName, NSString *code);
3.[strongSelf selectedCountryName:countryName andCode:code];//选择完国家之后的回调函数
- (void)selectedCountryName:(NSString *)countryName andCode:(NSString *)code {
self.countryCodeLabel.text = code;
NSString *placeholder = [NSString stringWithFormat:@"%@ %@", countryName, code];
self.phoneNumberTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:HintTextColor}];
}
这样就完成了