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
Related
I had develop a instant meaning checker like one in UIMENU Button,it working fine but since i had used the alert method to show the meaning,the appearnce of the alert is not good.can any one tell me how to get the appearance of that of the define method in UIMENU button
UIAlertView *av = [[UIAlertView alloc] initWithFrame:CGRectMake(0,0,300,200)];//[[UIAlertView alloc] initWithTitle:newstring message:nil delegate:nil cancelButtonTitle:#"ཕྱིར་འཐེན།" otherButtonTitles:nil];
av.title = newstring;
av.layer.backgroundColor=(__bridge CGColorRef)([UIColor colorWithRed:22/255.0f green:113/225.0f blue:9/255.0f alpha:1.0f]);
av.message = nil;
av.delegate = nil ;
[av addButtonWithTitle:#"ཕྱིར་འཐེན།"];
//[av addButtonWithTitle:#"OK"];
UITextView *v = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,200)]; //av.bounds];
v.text=newstring1;
[v setEditable:NO];
v.font=[UIFont fontWithName:#"Monlam Uni OuChan2" size:19];
[av setValue:v forKey:#"accessoryView"];
v.backgroundColor = [UIColor yellowColor];
[av show];
This is the code and the alert view that i got is
I'm assuming that you are using tap gesture to show the menu.
The following code displays a UIMenu of 100x100 with title as "Custom Action"
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
NSLog(#"tapGesture:");
// CGRect targetRectangle = self.tapView.frame;
CGRect targetRectangle = CGRectMake(100, 100, 100, 100);
[[UIMenuController sharedMenuController] setTargetRect:targetRectangle
inView:self.view];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Custom Action"
action:#selector(customAction:)];
[[UIMenuController sharedMenuController]
setMenuItems:#[menuItem]];
[[UIMenuController sharedMenuController]
setMenuVisible:YES animated:YES];
}
For a detailed version, have a look here.
As everyone's aware, UIAlertView is deprecated now and Apple wants us to use the new UIAlertController with the style UIAlertControllerStyleAlert. However, using the UIAlertControllerStyleAlert you cannot trigger the UIAlertAction with style UIAlertActionStyleCancel by tapping outside the alert view.
Does anyone know of a way to dismiss the alert view by tapping outside of it?
Cheers
You can add a separate cancel action with style UIAlertActionStyleCancel for the alertViewController so that when user taps outside, you would get the callback.
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Called when user taps outside
}]];
Try this code:
- (void)viewDidLoad {
[super viewDidLoad];
[self button];
}
- (void) button {
UIButton * AlertButton = [UIButton buttonWithType:UIButtonTypeSystem];
[AlertButton setTitle:#"Button" forState:UIControlStateNormal];
AlertButton.frame = CGRectMake((self.view.frame.size.width/2) - 50 , (self.view.frame.size.height/2) - 25, 100, 50);
[AlertButton addTarget:self action:#selector(Alert) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:AlertButton];
}
- (void)Alert {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController: alert animated: YES completion:^{ alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(DismissAlertByTab)]]; }];
}
- (void)DismissAlertByTab
{
[self dismissViewControllerAnimated: YES completion: nil];
}
I want to create action sheet with two buttons, but i want the buttons to have thumbnail icons, is that possible? any other solutions? maybe there is a way to customize the action sheet buttons.. like to design them separately?
this is my current action sheet method:
- (void)tapResponder {
NSLog(#"Tap!");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"this is a title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"one", #"two", nil];
[actionSheet showInView:self.view];
}
but i really like to stay with ios design so please give me a cool solution :))
thankssss!
Maybe this helps:
for (UIView *subview in actionSheet.subviews) {
if( [subview isKindOfClass: [UIButton class]] ){
UIButton* btn = (UIButton*) subview;
[btn setImage:[UIImage imageNamed: #"imageName"] forState:UIControlStateNormal];
}
}
Very possible, very easy to implement :
This works with the new UIAlertController as well :
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerSytleActionSheet];
//Add your Actions
...
UIAlertAction *snapshot = [UIAlertAction actionWithTitle:#"Snapshot" style:UIAlertActionSytleDefault handler:^(UIAlertAction *action) {
}
//create a UIImage to display since it's technically a tableView:
UIImage *snapshotImage = [UIImage imageNamed:#"snapshot.png"];
//add it to the UIAlertAction of your choosing
[snapshot setValue:snapshotImage forKey:#"image"];
...
[alertC addAction:snapshot];
[self presentViewController:alertC animated:YES completion:nil];
Prior to iOS 8 it can be done this way:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"this is a title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"one", #"two", nil];
[[[action valueForKey:#"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[[[action valueForKey:#"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:#"yourImage_Highlighted.png"] forState:UIControlStateHighlighted];
[actionSheet showInView:self.view];
A couple of notes about this, make sure you properly size your image, whatever the H x W demensions are is how huge the row will become once it's inserted into the Action Sheet. So use something like 30x30. Also, whatever color your image is will default to the blue tint. Additionally, this isn't considered public API, apps do get approved this way, but you do run the risk
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
:)
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);