如何通知视图控制器已在 UIPickerView 中选择了一行?

发布于 2024-11-14 01:51:22 字数 235 浏览 5 评论 0原文

当在 UIPickerView 中选择一行时,如何通知我的视图控制器并向其传递字符串?

我在常规视图中有一个自定义 UIPickerView,并且在不同的类中有一个自定义数据源/委托

如何在 pickerView:didSelectRow:inComponent: 委托方法期间通知我的视图?并将选定的字符串传递给视图控制器?

How do I notify my view controller and pass it the string when a row was selected in my UIPickerView?

I have a custom UIPickerView in regular view and I have a custom datasource/delegate in a different class.

How do I notify my view during the pickerView:didSelectRow:inComponent: delegate method? and pass the selected string to the view controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

压抑⊿情绪 2024-11-21 01:51:22

符合选择器委托的类将实现

pickerView:didSelectRow:inComponent:

以了解在选择器中选择了哪一行(或字符串)。

如果外部世界的某个其他类想知道选择了哪一行,那么该类又需要通知。这将创建一个授权链。

例如,如果 pickerClass 是实现委托方法的类。如果其他一些类(比如outsideClass)想要知道选择了哪一行,那么您可以创建pickerClass的outsideClass委托并将此消息传递给它。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[_pickerClassDelegate pickerClass:self didSelectRow:row inComponent:component];
}

The class that is conforming to the picker delegates would implement

pickerView:didSelectRow:inComponent:

to know which row (or string) was selected in the picker.

If some other class from the outside world wants to know which row was selected, then this class in turn needs to inform. This will create a chain of delegation.

For example, if pickerClass is the class that implements the delegate method. If some other class (say outsideClass) wants to know about which row was selected, then you would make this outsideClass delegate of pickerClass and pass on this message to it.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[_pickerClassDelegate pickerClass:self didSelectRow:row inComponent:component];
}
青萝楚歌 2024-11-21 01:51:22

为此,您必须在自定义类中创建一个方法,如下所示:

.h 文件
父母身份证;

.m 文件

 -(void)initWithPageNumber:(id)pidParent{
      parent=pidParent
}

然后您以编程方式设置委托方法,如下所示:
uipickerview.delegate = 父级;
uipickerview.datasource = 父级;

希望它会有所帮助。

如果有困难请告诉我。

For that, you have to create one method in custom class like below:

.h file
id parent;

.m file

 -(void)initWithPageNumber:(id)pidParent{
      parent=pidParent
}

Then you have set the delegate method programatically like below:
uipickerview.delegate = parent;
uipickerview.datasource = parent;

Hope it will be helpful.

Let me know in case of difficulty.

蔚蓝源自深海 2024-11-21 01:51:22

您可以通过使用 NSNOtification centet 发布和观察通知或创建委托协议来完成此操作。研究委托协议,这是在 Objective-C 中学习的一件好事。

You can do this by either using posting and observing notification using NSNOtification centet or by creating a delegate protocol. Study delegate protocols that is a good thing to learn in objective-c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文