UIpickerview hidden behind actionsheet - ios

I am new to xcode and first time at stackoverflow. I have a pickerview as subview of UIActionSheet. Pickerview is used to select $ amount for a field.
On iOS >=4.2, pickerview shows on top of actionsheet as expected. However, with ios 4.0/1, the actionsheet covers pickerview, though i can select the values.
The code
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:#"Select Bill Amount Before Tips" delegate:self cancelButtonTitle:nil destructiveButtonTitle:#"Done" otherButtonTitles:nil];
[sheet addSubview:pickerview];
pickerview.showsSelectionIndicator = YES;
[sheet showInView:self.tabBarController.view];
/******** action sheet size **********/
CGRect menuRect = sheet.frame;
CGFloat orgHeight = menuRect.size.height;
menuRect.origin.y -= 216;
menuRect.size.height = orgHeight+216;
sheet.frame = menuRect;
/**************************************/
CGRect pickerRect = pickerview.frame;
pickerRect.origin.y = orgHeight;
pickerview.frame = pickerRect;
// [self.tabBarController.view bringSubviewToFront:pickerview];
[sheet release];
return NO;
}
thanks for helping out.

Related

How to add date picker to UIAlertController(UIAlertView) in Swift?

I have an application in which I need to display a popup to pick a date and write that date back to the view controller.
I have searched online a lot but wasn't successful in finding out a relevant solution.
Any kind of help is much appreciated.
Do not use any kind of UIAlertView or UIAlertController. Make your own view and pop it up (using, probably, a presented view controller). The view can be small, like an date picker, and you can put a shadow view behind it, like an alert does.
This is my Code :
- (id)initWithDatePicker:(NSString*)title parentView:(UIView*)parentView {
self = [super init];
if (self) {
datePickerView = [[UIDatePicker alloc] init];
datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
if (IS_IOS8_AND_UP) {
alertViewController = [UIAlertController
alertControllerWithTitle:EMPTY_STRING
message:title
preferredStyle:UIAlertControllerStyleActionSheet];
UIView* aboveBlurLayer = alertViewController.view.subviews[0];
[aboveBlurLayer addSubview:datePickerView];
[aboveBlurLayer setWidth:SCREEN_WIDTH - 16];
[datePickerView setWidth:SCREEN_WIDTH - 16];
[alertViewController.view setWidth:SCREEN_WIDTH - 16];
[alertViewController.view setBackgroundColor:[UIColor whiteColor]];
UIAlertAction* alertAction =
[UIAlertAction actionWithTitle:EMPTY_STRING
style:UIAlertActionStyleDefault
handler:nil];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[alertViewController addAction:alertAction];
[datePickerView setBackgroundColor:[UIColor whiteColor]];
[aboveBlurLayer addSubview:datePickerView];
} else {
actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet addSubview:datePickerView];
}
[self addToolBar];
isDatePicker = YES;
parent = parentView;
}
return self;
}
On Tool bar I have two buttons Done and Cancel
On Done i send back a call via delegate with selected date and on cancel I dismiss.
This code is for both iOS 7 and iOS 8

Add UISlider on UIAlertView

I want to add a UISlider on a UIAlerView but I am not able to do that. I also google that but I found only two answers and both of them are not showing any any result here is code what I found and applied in my project:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"ok", nil];
alertView.title = #"title";
CGRect frame = CGRectMake(alertView.frame.origin.x+10,alertView.frame.origin.y+20,100, 100.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 10.0;
slider.continuous = YES;
slider.value = 25.0;
[alertView addSubview:slider];
[alertView show];
I think the frame of slider is not proper thats why its not being displayed.
can anyone help me in that.....
Thank you in advance...
You are adding the Slider in Alert view not in parent view of Alert view.
So your frame value will add the slider some where wide in alert view, result will be hidden on alert view.
so replace this line
CGRect frame = CGRectMake(alertView.frame.origin.x+10,alertView.frame.origin.y+20,100, 100.0);
To
CGRect frame = CGRectMake(10,20,100, 100.0);
The UIAlertView is also a UIView. I indeed, you could add the subview in it. But Apple says
"The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified."
So Better to have custom view which will act as alertview.Like this one

UITextField In UIAlert Input Not Working iOS 7

I am currently using the following to use a popup alert which allows the user to set a delay in seconds;
The input itself works fine, but the number is not remembered, so if I put 5, or 10, when I press 'set' it just ignores that value and goes to instant recording again. here is the IBAction to call the setDelay, and below it, I have posted the Alertview
-(IBAction)setDelay:(id)sender
{
// open a alert with text field, OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
CGRect frame = CGRectMake(14, 45, 255, 23);
if(!delayTextField) {
delayTextField = [[UITextField alloc] initWithFrame:frame];
delayTextField.borderStyle = UITextBorderStyleBezel;
delayTextField.textColor = [UIColor blackColor];
delayTextField.textAlignment = UITextAlignmentLeft;
delayTextField.font = [UIFont systemFontOfSize:14.0];
delayTextField.backgroundColor = [UIColor redColor];
delayTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
delayTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; // use the default type input method (entire keyboard)
delayTextField.returnKeyType = UIReturnKeyDefault;
delayTextField.delegate = self;
delayTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
}
else
{
delayTextField.text=#"";
}
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
[alert release];
}
//
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
self.delayTextField.text=#"";
}
}
}
From iOS 7 you can't add subviews to UIAlertView anymore -
UIAlertView addSubview in iOS7.
But if you need just simple UITextField in your UIAlertView you can just set UIAlertView style to UIAlertViewStylePlainTextInput and retrieve value from that field later. Eg.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
then in your delegate method:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
UITextField *alertViewTextField = [alertView textFieldAtIndex:0];
//Do something with alertViewTextField.text value
}
}
}
#Guferos answer is absolutetly correct. It doesn't work because iOS 7 doesnt support this.
But in case you need more than a simple TextView, you can use a framework such as https://github.com/jdg/MBProgressHUD
:)

Retrieving the value from UIAlertView textbox in iOS app

I have an iOS app that I recently updated to deal with the UIAlertView / SubView issue that causes the textboxes to render as clear or white (or not render at all, not sure which). In any case, this is a relatively simple question as I'm kind of new to Obj-C, but how do I get the value of the new textbox from another call in the app?
Here is my UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password"
message:#"Enter your Password\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Login", nil];
alert.frame = CGRectMake( 0, 30, 300, 260);
It used to be stored as a UITextField, and then added to the UIAlertView as a subview:
psswdField = [[UITextField alloc] initWithFrame:CGRectMake(32.0, 65.0, 220.0, 25.0)];
psswdField.placeholder = #"Password";
psswdField.secureTextEntry = YES;
psswdField.delegate = self;
psswdField.tag = 1;
[psswdField becomeFirstResponder];
[alert addSubview:psswdField];
[alert show];
[alert release];
This is all commented out now, and instead I have it rewritten as:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
This is how I used to retrieve the value:
[psswdField resignFirstResponder];
[psswdField removeFromSuperview];
activBkgrndView.hidden = NO;
[activInd startAnimating];
[psswdField resignFirstResponder];
[self performSelectorInBackground:#selector(loadData:) withObject:psswdField.text];
Now I'm a bit confused as to how I get the value from that textbox to send to loadData.
You don't want to add your own text field to the alert view. You're not supposed to directly add subviews to a UIAlertView. There is an alertViewStyle property on UIAlertView that you want to set to UIAlertViewStyleSecureTextInput, which will add a text field for you. So you would set it with a line like this:
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Then you will retrieve the value in this text field using the delegate method - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex, which you must add to the class that you're setting as your UIAlertView delegate. Here is an example implementation of that delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
// Make sure the button they clicked wasn't Cancel
if (buttonIndex == alertView.firstOtherButtonIndex) {
UITextField *textField = [alertView textFieldAtIndex:0];
NSLog(#"%#", textField.text);
}
}

Alert view shifted upwards due to the addition of Text field in it

I am creating an Alert view on click of a button. I have placed a UITextfield in it. But the Alertview is shifted upwards in the screen. I am not able to bring it back at the center of the screen.
Here is my code and Screenshot.
and the code for creating this Alert view is this:-
- (IBAction)btn_cancelAppointment_click:(id)sender
{
// Here We are Creating Alertview which asks for
UIAlertView* cancelAlert = [[UIAlertView alloc] init];
[cancelAlert setDelegate:self];
[cancelAlert setTitle:#"Are you sure you want to request cancellation of this appointment?"];
[cancelAlert setMessage:#" "];
[cancelAlert addButtonWithTitle:#"NO"];
[cancelAlert addButtonWithTitle:#"YES"];
UITextField * txt_cancelNote = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 88.0, 245.0, 30.0)];
[txt_cancelNote setBackgroundColor:[UIColor whiteColor]];
[txt_cancelNote setBorderStyle:UITextBorderStyleRoundedRect];
[txt_cancelNote setPlaceholder:#"Enter cancellation note"];
txt_cancelNote.tag = 11;
[cancelAlert addSubview:txt_cancelNote];
cancelAlert.delegate = self;
[cancelAlert show];
}
So, if somebody can tell me how to bring it back to the center of the screen. Any help will be highly appreciated.
Give a try by setting the transform property of the UIAlertView
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Test" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
alertView.transform = CGAffineTransformMakeTranslation(x, y);

Resources