How to add button to UIPopoverController ?
I am attaching a picture for able to understand very clear.
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,400 ,44)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[popoverView addSubview:toolbar];
[toolbar release];
picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 400, 340);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[popoverView addSubview:picker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController setPopoverContentSize:CGSizeMake(400, 264) animated:NO];
[popoverController presentPopoverFromRect:btn.frame inView:scr_AddLocation permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
here I m adding Picker To Popover When We Pressed Button .
The cancel button (if that is what you are referring to) is the rightBarButtonItem of the UINavigationController's navigation bar. You can set it to be a standard UIBarButtonItem (as is done here) or a custom one (whose view you can create).
Related
I'm just a beginner in iPhone programming.
I have added 3 uipickers in one view.
First picker for employee.
Second picker is for product.
Third picker for customer.
I have created 3 tables in sqlite for the above.
And I have to load the values to pickers from the tables.
Have I to keep 3 pickers in XiB file?
I want the pickers to be displayed when i click on the table cell.(I have a table view obviously...where I will choose the desired value from the picker)
For time being I have added the array statically. But I wanted to load it from the sqlite database.
How to connect?
How to display the values?
What should I do?
Can you please help me.
I've been meddling with it for 4 days. Can Someone please help....
Cathi
Create 3 UiPickerView using Code after the Database read is done
Set The values according to the database
Set separate Tags (1,2,3) for each UIPicker. Then you can get it again every where.
Set the UiPickerView delegates.
Use [yourPicker selectRow:10 inComponent:0 animated:YES]; to set picker selected value
Sample Code to Add UIPicker
-(void)displayPicker{
#try {
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
cancelBtn.tag = 1;
[barItems addObject:cancelBtn];
[cancelBtn release];
cancelBtn = nil;
/* Uncomment this line to add label to Timer/Counter.
NSString *popUpTitleText = [self getPopUpTitleText:#"My Text"];
UIBarButtonItem *titleBtn = [[UIBarButtonItem alloc] initWithTitle:popUpTitleText style:UIBarButtonItemStylePlain target:nil action:nil];
[barItems addObject:titleBtn];
[titleBtn release];
titleBtn = nil;
*/
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flexItem.width = 64;
[barItems addObject:flexItem];
[flexItem release];
flexItem = nil;
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
doneBtn.tag = 1;
[barItems addObject:doneBtn];
[doneBtn release];
doneBtn = nil;
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
barItems = nil;
CGRect pickerFrame = CGRectMake(0, 40, 0, 216);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.tag = 1;
pickerView.dataSource = self;
pickerView.delegate = self;
CGRect pickerRect = pickerView.bounds;
pickerView.bounds = pickerRect;
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
pickerView.frame = CGRectMake(0, 44, 200, 216);
[pickerView selectRow:1 inComponent:0 animated:YES]; //set your selected (Database) value here.
[popoverView addSubview:pickerToolbar];
[popoverView addSubview:pickerView];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 244);
//create a popover controller
popoverController.delegate = nil;
if (popoverController.popoverVisible == YES) {
[popoverController dismissPopoverAnimated:YES];
}
[popoverController release];
popoverController = nil;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:yourFrame(CGrect)
fromView:[yourView superview]];
popoverRect.origin.y = popoverRect.origin.y + 15;
popoverController.delegate = self;
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = float yourXvalue;
[popoverController presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
[pickerToolbar release];
[pickerView release];
popoverView = nil;
popoverContent = nil;
pickerToolbar = nil;
pickerView = nil;
}
#catch (NSException * ex) {
NSLog(#"Exception in YourClass Method: displayPicker() Name:%# Reason:%#",[ex name],[ex reason]);
}
}
i add some viewcontroller to the tab bar controller and in viewcontroller i add 1 textfield so when i click on text field i call UIactionsheet. the action sheet contain 2 sub parts (1. UIpickerview 2. UItoolbar)... i face some issue like when i load UIactionsheet it works fine but i am not able select any row.. or not click on done button for dissmiss actionsheet. i put some code here..
in tabbarcontroller.m
-(void)LoadActionSheet
{
actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionsheet.actionSheetStyle = UIActionSheetStyleDefault;
actionsheet.delegate = self;
}
in viewcontroller.m
-(void) Comboaction:(NSMutableArray *)DataArray
{
EditTabController *obj_tab = [[EditTabController alloc] init];
[obj_tab LoadActionSheet];
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(100, obj_tab.actionsheet.frame.origin.y-15, 200, 44)];
Toolbar.barStyle = UIBarStyleBlack;
[Toolbar sizeToFit];
[Toolbar bringSubviewToFront:obj_tab.actionsheet];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DissmissPickerview)];
[barItems addObject:doneBtn];
[Toolbar setItems:barItems animated:YES];
[obj_tab.actionsheet setBounds:CGRectMake(0, obj_tab.tbar.frame.origin.y-100, 325,40)];
picker = [[UIPickerView alloc] init];
[picker setFrame:CGRectZero];
picker.tag = 101;
picker.delegate = self;
picker.dataSource = self;
picker.showsSelection
Indicator = YES;
[picker setShowsSelectionIndicator:YES];
[picker setBackgroundColor:[UIColor blackColor]];
// [picker setFrame:CGRectMake(0,Toolbar.frame.size.height+1, 320, 360)];
[picker bringSubviewToFront:obj_tab.actionsheet];
[obj_tab.actionsheet setDelegate:self];
[obj_tab.actionsheet addSubview:Toolbar];
[obj_tab.actionsheet addSubview:picker];
[obj_tab.actionsheet setBounds:CGRectMake(0, 0, 320, 50)];
[obj_tab.actionsheet showInView:[UIApplication sharedApplication].keyWindow];
}
and all pickerview delegate methods.. it works fine in simple viewcontroller. but not work properly in tabbarcontroller..
I have a UIButton that when clicked, displays a UIPopover with a UIDatePicker in it.
It is supposed to display under button for date of birth, but it is covering it instead.
This is the code I have:
- (IBAction)dateOfBirthButtonPressed:(id)sender{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePickerTemp = [[UIDatePicker alloc]init];
datePickerTemp.frame=CGRectMake(0,44,320, 216);
datePickerTemp.datePickerMode = UIDatePickerModeDate;
datePickerTemp.maximumDate = [NSDate date];
self.datePicker = datePickerTemp;
[popoverView addSubview:self.datePicker];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0 ,320, 40);
toolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *toolbarItems = [NSMutableArray array];
UIBarButtonItem *cancelButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(datePickerCancelButtonClicked)];
UIBarButtonItem *doneButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(datePickerSaveButtonClicked)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbarItems addObject:cancelButton1];
[toolbarItems addObject:space];
[toolbarItems addObject:doneButton1];
toolbar.items = toolbarItems;
[popoverView addSubview:toolbar];
[cancelButton1 release];
[space release];
[toolbar release];
popoverContent.view = popoverView;
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
self.datePopoverController = popoverController;
[popoverContent release];
[popoverView release];
[self.datePopoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
NSLog for frame:
DOB button: {{160, 129}, {329, 37}}
Last name label: {{160, 77}, {329, 31}}
Is your button a subview of your view, or buried within another subview? If it is not a direct subview of your view then your coordinate space is off. Try changing your code to:
CGRect buttonRect = [self.dateOfBirthButton convertRect:self.dateOfBirthButton.frame toView:self.view];
[self.datePopoverController presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You can try either one of these to see that fix the problem. The second one fix my problem that is similar to yours.
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
or
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I'm using the following code to display a UIDatePicker within a UIPopover that is displayed when a user clicks a UIButton.
The problem is, it is being displayed in an awkward position and I want to add a UIToolBar above the datePicker where there is extra space. This will have cancel and done button. How can I accomplish this? The button that it is displayed from is the Date of Birth one.
- (IBAction)dateOfBirthButtonPressed:(id)sender{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
// [datePicker addTarget:self action:#selector(dateChanged) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverContent release];
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Is there a reason you couldn't just use a UIToolbar?
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 44.0)];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel
target: self
action: #selector(cancel)];
UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target: nil
action: nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone
target: self
action: #selector(done)];
NSMutableArray* toolbarItems = [NSMutableArray array];
[toolbarItems addObject:cancelButton];
[toolbarItems addObject:space];
[toolbarItems addObject:doneButton];
[cancelButton release];
[doneButton release];
[space release];
toolbar.items = toolbarItems;
Then just add the toolbar to your view. Make sure to size it correctly and implement the done and cancel selectors.
[popoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Use sender in place of self.view
[popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}