如何从拥有 UIPickerView 的视图控制器外部访问 UIPickerView 值?

How to Access a UIPickerView Value from Outside the View Controller that Owns the UIPickerView?

我在 ViewControllerA(在 Xib 文件中)中声明了一个选择器视图。此 Xib 作为自定义单元格加载到 ViewControllerB 的 tableView 中。如果我更改了 Picker View 的值,我如何在 ViewControllerB 中访问这个更改后的值。

适应性更强的方式: 使用 NSNotification。

在ViewControllerA中实现UIPickerViewDelegate方法:

- (void)pickerView:(UIPickerView * _Nonnull)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  //get the data of the picker and save it in the dictionary
  NSMutableDictionary *pickerData =[NSMutableDictionary new]
  [pickerData setValue:[yourArrayForPicker objectAtIndex:row] forKey:@"value"];
    //create and send the notification with the dictionary
  [[NSNotificationCenter defaultCenter] 
                 postNotificationName:@"XYYourNotification"
                 object:pickerData];
  }

比在 ViewControllerB 中注册通知的侦听器:

//put this code where you want (maybe in view did load)
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserverForName:@"XYYourNotification"
                        object:nil
                         queue:nil
                    usingBlock:^(NSNotification *notification)
    {
//this code will be called when the notification is received
          NSDictionary* info = [notification userInfo];
            NSLog(@"selected :%@", [pickerData valueForKey:@"value"]);
    }];

您可以使用各种方式从 viewController A 在 ViewController B 上获取选择器值,如我提到的静态 getter setter ,应用程序委托 class这里使用 appDelegate class 在 AppDelegate.h class 中声明具有您选择的数据类型的属性,因为我将其视为 NSString

@property (nonatomic, strong) NSString* pickerValue;

在 ViewController 中的 AppDelegate class 上获取实例 A 和 B 在 vi​​ewDidLoad

self.appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;

在选取器委托方法中将值设置为

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component

{ self.appDelegate.pickerValue = self.pickerData[行];

}

在 viewController B 中,您可以使用来自

的用户值
 NSLog(@"ViewControllerB::picker valuer::%@",self.appDelegate.pickerValue);

您可以通过在 ViewControllerA 上公开 属性 来访问 UIPickerView 的值...如果在特定情况下 viewControllerB 需要知道该值, 在 viewControllerB 的控制下, 然后 viewControllerB 可以检查 viewControllerA 上的 属性 在那个时候...

但是,也许您问的是关于 通信 的更笼统的问题 - 具体示例 ViewControllerB 需要了解对 [=13] 的更改=] 里面 ViewControllerA...

Communication Patterns 上的这篇文章值得一读。它涵盖 KVONotificationsDelegationBlocksTarget/Action.

文章中间附近有一个流程图,可以帮助评估在给定情况下使用哪种沟通策略。

根据您所写的内容,听起来您可以使用 KVO(键值观察)或委托。

我倾向于在一个 UIViewContoller 想知道另一个 UIViewController 所做的更改的情况下使用委托,例如当 viewControllerC 呈现 viewControllerD - 并想了解 viewControllerD.

中所做的更改

在您的情况下,您可以按照以下方式使用委托方法:

- (void)pickerViewValueDidChange:(NSString*)newValue;

委托方法 将是 @protocol 的一部分。类似于:

@protocol ABCPickerViewDelegate
- (void)pickerViewValueDidChange:(NSString*)newValue;
@end

如果您不熟悉,请参阅 Working with Protocols...

viewControllerB 将符合该协议。 viewControllerA 将有一个符合协议的 属性,例如:

@property (weak, nonatomic) id <ABCPickerViewDelegate> pickerDelegate;

然后,当 UIPickerView 值在 viewControllerA 内发生变化时 - 它可以调用委托方法...然后,viewControllerB 就会知道发生了变化以及值为.