tags:

views:

63

answers:

2

Hi!I want to add Toolbar on the upperside of PickerView and want to add one BarButton on the Toolbar Dynamically and on the click event of that button i want to dismiss the Picker as well as toolbar so please help me in this task...Thanks in Advance..

A: 

You can create the UIToolBar and its buttons this way -

UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 324.0f, 320.0f, 44.0f)];
    [toolBar setBarStyle:UIBarStyleBlackTranslucent];

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPicker:)]; 

    [toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneBtn, nil] animated:YES];
    [flexibleSpace release];
    [editButton release];

You can add the done button and call a method on tap of this button, in this method you can dismiss the UIPickerView.

Saurabh
Yes,Thanks for Reply.....
Ankit Vyas
i want black barbutton instead of Blue.
Ankit Vyas
According to iPhone HIG "Done" button should be blue. but if you want to add custom button, create a view then add a button in this view and add this view in barbutton item like this - UIView *leftBarButtons = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 200.0f, 34.0f)];// add button in this leftBarButton viewUIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButtons];
Saurabh
+1  A: 

Mr. Ankit - Try to build your objects using through Interface builder because there is no need to maintain all the objects which are being constructed interface builder ( according to me - yet not sure - please add comment, if I am wrong anywhere ).

See,the Attached Snapshots to place Buttons on Your toolbar. Just make connections to your class IBOutlet objects.

I know - you have mentioned "Dynamic" word in your question.

You just need to add subview in to your view.

For example

 - (void)viewDidLoad {
     [super viewDidLoad];
     [self.view addSubview:toolBarView];  // add whenever required.
     // alternate option
     toolBarView.hidden=YES; // or NO whatever required.
}

alt text

sugar
Thanks man!i know you are Genius.....
Ankit Vyas