Cannot add a UIDatePicker to UIActionSheet - ios

I want to add a UIDatePicker to a UIActionSheet, the code is below:
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:#"DEMO" delegate:self cancelButtonTitle:#"Cancle" destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Ratings"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet sendSubviewToBack:pickerView];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[actionSheet release];
The question is why this code cannot work in iPad mode while it is ok in iphone mode

I would try using EAActionSheetPicker. It handles that for you.

Related

Add UIPickerView to a UIAlertController

I'm trying to add a UIPickerVivew to a UIAlertController. I tried to do the following:
- (void)alertPickerView
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title" delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil, nil];
actionSheet.delegate = self;
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:pickerView];
}
When I called the method, the pickerView and actionSheet didn't come up.
(I would rather a UIAlertController, but if that's not possible, I'll go with a actionsheet.)

How to change a UIAlertView's button background color?

I've created UIAlertView with multiple buttons with the following code:
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
alert.tag = TAG_ALERT0;
alert.title = #"Notes";
alert.delegate = self;
[alert addButtonWithTitle:#"Add Note"];
[alert addButtonWithTitle:#"Show Notes"];
[alert addButtonWithTitle:#"Cancel"];
[alert show];
I'm wondering if it is possible to add a different background color to each button?
Yes, you can but should not modify the look of UIAlertView since Apple hates that shit.
Instead, create your own UIView that mimics the alertView.
Also... does the initWithFrame method even make a difference?
Example... Try:
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
It won't make a difference.
So... I'd suggest:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Notes"
message:nil
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Add Note",#"Show Notes",nil];
[alert setTag:TAG_ALERT0];
[alert show];
But since you asked for sample code, the following example will be simple and to the point:
STATUTORY WARNING:
Apple doesn't like this
Doesn't work in iOS7
Example:
-(void)willPresentAlertView:(UIAlertView *)alertView
{
UILabel *theTitle = [alertView valueForKey:#"_titleLabel"];
[theTitle setTextColor:[UIColor orangeColor]];
NSMutableArray *arrButtons = [alertView valueForKey:#"_buttons"];
for (UIView *vwCurrent in arrButtons) {
[vwCurrent setBackgroundColor:[UIColor redColor]];
}
}

uipopover issues while click and open the camera for ipad

i have a popoverview issues while click and open the camera for ipad,i wrote code like that
-(IBAction)business_takephotobtnClicked // click the button show the popoverview
{
NSLog(#"business_takephotobtnClicked");
appdelegate.takePhoto=2;
popover = [[UIPopoverController alloc]
initWithContentViewController:imgclass];
popover.popoverContentSize = CGSizeMake(138,66);
[popover presentPopoverFromRect:popbtn_business.bounds inView:popbtn_business
permittedArrowDirections:UIPopoverArrowDirectionUp +
UIPopoverArrowDirectionLeft
animated:YES];
}
-(IBAction) takePhoto:(id)sender // to open the camera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
self.contentSizeForViewInPopover=CGSizeMake(138,66);
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
before click the button the popover like that show
while click the take photo(Neem foto button).THE POPOVERVIEW SIZE IS AUTOMATICALLY EXTENTED like taht
But i need a same size popoverview while open the camera also
Thanks in Advance......
instead of using XIB use to create the camera view programatically and do like the following
-(IBAction)popbtn_Click:(id)sender
{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 230, 180)];
popoverView.backgroundColor = [UIColor whiteColor];
take_btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[take_btn setTitle:#"Take" forState:UIControlStateNormal];
take_btn.frame=CGRectMake(2,2, 250, 60);
[take_btn addTarget:self action:#selector(take_btnclick:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:take_btn];
}
-(void)take_btnclick:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:self.UIPicker animated:YES];
[popoverController dismissPopoverAnimated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Camera is not available" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
if (popoverController != nil)
{
[popoverController dismissPopoverAnimated:YES];
}
}

Selection indicator not showing in picker view inside an action sheet

Here's my implementation, I'm basically creating the action sheet and picker view on the fly. The problem is the indicator to show which item you have selected isn't showing.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100, 320, 216)];
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0, 320, 411)];
It looks like you're missing this line:
[picker setShowsSelectionIndicator:YES];

Is having a UIPickerView in a UIActionSheet acceptable?

Is it acceptable (aka would Apple consider it acceptable) to have a UIPickerView in a UIActionSheet?
Yes, It is totally acceptable. To some degree its even encouraged.
Example code: HERE by Erica Sadun in her book iPhone Developer's cookbook. Chapter 11 Recipe 21
Yes. It is acceptable.
set the frame of the UIPickerView
add the UIPickerView to the actionView
As I remember, actionView will be full screen in this case
UIActionSheet *actionView = [[UIActionSheet alloc] initWith...];
UIPickerView *pickerView = [UIPickerView alloc] init...];
pickerView.frame = CGRect(....);
[actionView addSubview:pickerView];
[pickerView release];
[actionView showInView:theView];
[actionView release];
This should do it :
NSString *title = #"\n\n\n\n\n\n\n\n\n\n\n\n"; // <--- Taken from Erica Sadun´s CookBook
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet setBounds:CGRectMake(0,0,320,485)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
[actionSheet addSubview:picker];
[actionSheet showFromTabBar:[[self tabBarController] tabBar]];
Please remember that you have to set UIPickerView´s dataSource and delegate .

Resources