简体   繁体   English

如何在UIPickerView上添加UIToolbar? (iPhone / ipad公司)

[英]How to add a UIToolbar on top of UIPickerView? (iPhone/iPad)

In my app I have a button that when pressed should present a UIPickerView with a UIToolbar on top of it. 在我的应用我有一个按钮,按下应该呈现时UIPickerView一个UIToolbar在它上面。 When the user has selected the value in the UIPickerView they then press a button in the UIToolbar which dismisses both the UIPickerView and UIToolbar . 当用户在UIPickerView选择了值时,他们按下UIToolbar一个按钮,该按钮同时取消UIPickerViewUIToolbar

The problem I'm having is that I can't figure out a way to add the UIToolbar to the UIPickerView . 我遇到的问题是我无法找到将UIToolbar添加到UIPickerView Normally I would have a setup where a value in a UITextField is impacted by the selection in the UIPickerView . 通常我会有一个设置,其中UITextField中的值受到UIPickerView的选择的影响。 This way I can simply set the UIToolbar as the inputAccessoryView , however, this time round I don't have a UITextField . 这样我就可以简单地将UIToolbar设置为inputAccessoryView ,但是,这次我没有UITextField

Please can someone advise on how to add the UIToolbar on top of the UIPickerView ? 有人可以建议如何在UIPickerView上添加UIToolbar吗?

Best Solution for this is make use of UIActionSheet . 最佳解决方案是使用UIActionSheet

Just Create Global of UIActionSheet ,add Your UIToolbar and UIPickerview to UIActionSheet as subview. 只需创建UIActionSheet全局,将您的UIToolbarUIPickerview添加到UIActionSheet作为子视图。

At last add UIActionSheet in subview of your main View. 最后在主视图的子视图中添加UIActionSheet

Hope It will help you.!! 希望它会帮助你。!!

You need to add the toolbar to the parent container view of the UIPickerView. 您需要将工具栏添加到UIPickerView的父容器视图。 So if you know what that view is, then you can add the toolbar to that. 因此,如果您知道该视图是什么,那么您可以将工具栏添加到该视图中。 If the container is a NavigationViewController you can add tools to it's toolbar. 如果容器是NavigationViewController,您可以将工具添加到它的工具栏。

If you don't have a parent view, then create one -- eg, add a new view class -- give it a toolbar and add the PickerView under it in the same view. 如果您没有父视图,那么创建一个 - 例如,添加一个新的视图类 - 给它一个工具栏并在同一个视图中添加PickerView。

To synchronize things in the interface, it is often easier to use NSNotifications: 要同步界面中的内容,通常更容易使用NSNotifications:

In the picker view... 在选择器视图中......

NSArray *keys = [NSArray arrayWithObjects:@"myParameterName", nil];
NSArray *objects = [NSArray arrayWithObjects: myObject, nil];
NSDictionary *myDictionaryOfParameters = [NSDictionary dictionaryWithObjects: objects forKeys: keys];

[[NSNotificationCenter defaultCenter] postNotificationName:@"myPickerViewChanged" object:nil userInfo: nil];

Then in the toolbar add an observer... 然后在工具栏中添加一个观察者......

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(refreshView) name:@"myPickerViewChanged" object: nil];

//   ...

- (void) myPickerViewChanged: (NSNotification *) note  {
    NSDictionary * myDictionaryOfParameters = note.userInfo;
// Enable toolbar buttons, etc.
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM