user843337
user843337

Reputation:

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. 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?

Upvotes: 0

Views: 1446

Answers (2)

Chad Celsius
Chad Celsius

Reputation: 31

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.

Upvotes: 1

Chirag Lukhi
Chirag Lukhi

Reputation: 1546

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.!!

Upvotes: 1

Related Questions