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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:13:58

问题


In my app I have a button that when pressed should present a UIPickerView with a UIToolbar on top of it. 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.

The problem I'm having is that I can't figure out a way to add the UIToolbar to the UIPickerView. Normally I would have a setup where a value in a UITextField is impacted by the selection in the UIPickerView. This way I can simply set the UIToolbar as the inputAccessoryView, however, this time round I don't have a UITextField.

Please can someone advise on how to add the UIToolbar on top of the UIPickerView?


回答1:


Best Solution for this is make use of UIActionSheet.

Just Create Global of UIActionSheet,add Your UIToolbar and UIPickerview to UIActionSheet as subview.

At last add UIActionSheet in subview of your main View.

Hope It will help you.!!




回答2:


You need to add the toolbar to the parent container view of the 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.

If you don't have a parent view, then create one -- e.g., add a new view class -- give it a toolbar and add the PickerView under it in the same view.

To synchronize things in the interface, it is often easier to use 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.



来源:https://stackoverflow.com/questions/10784720/how-to-add-a-uitoolbar-on-top-of-uipickerview-iphone-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!