Adding TextField to UIAlertView - iphone

I need to add a TextField to an UIAlertView. I understand that apple discourage this approach. So is there any library that i could make use of to add a TextField to a UIAlertView look-alike frame ?

For iOS5:
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Please enter someth" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
Also, you 'll need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.

Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStyle which can be set to the following parameters:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput being the one you want.
Messing with the view hierarchy as described above is strongly discouraged by Apple.

I'm using BlockAlertsAndActionSheets instead of the Apple components for AlertViews and ActionSheets as I prefer the blocks-approach. Also contains a BlockTextPromptAlertView in the source, which might be what you want. You can replace the images of that control to get the Apple-style back.
Project on gitgub
Tutorial which gets you started
Example:
- (IBAction)newFolder:(id)sender {
id selfDelegate = self;
UITextField *textField;
BlockTextPromptAlertView *alert = [BlockTextPromptAlertView promptWithTitle :#"New Folder"
message :#"Please enter the name of the new folder!"
textField :&textField];
[alert setCancelButtonWithTitle:#"Cancel" block:nil];
[alert addButtonWithTitle:#"Okay" block:^{
[selfDelegate createFolder:textField.text];
}];
[alert show];
}
- (void)createFolder:(NSString*)folderName {
// do stuff
}

Try something like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Title"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];

As of iOS 8 UIAlertView has been deprecated in favor of UIAlertController, which adds support for adding UITextFields using the method:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
See this answer for an example.

You can try:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Your title here!" message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:testTextField];
[myAlertView show];
[myAlertView release];
Follow this link for detail.

first of All Add UIAlertViewDelegate into ViewController.h File like
#import <UIKit/UIKit.h>
#interface UIViewController : UITableViewController<UIAlertViewDelegate>
#end
and than Add Below Code where you wants to alert Display,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
and it's delegate method which returns what input of UItextField
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}

adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne Hartman' Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(#"self.userNumber: %#",self.userNumber);
} else {
NSLog(#"null");
}
}
}

refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.html this is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.

Related

How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)

I am trying to create a UIAlertView with three buttons (which will be stacked). I would like the Cancel button to be in the middle, between the two other buttons. I have tried setting the cancelButtonIndex to 1, but if there are two other buttons, it simply places them at indexes 0 and 1. I know I could just change the names of the buttons, but I want the darker blue formatting of the cancel button.
EDIT: **
Please note - I know how to get the three buttons with the titles in the correct order, but only if all three buttons essentially look like 'other' buttons; I want the cancel button to have the cancel button dark blue background so that it will look like a regular cancel button.
**
I've tried
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:button1Title,button2Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:button2Title];
[alert show];
and
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:addButtonWithTitle:button1Title,nil] autorelease];
alert.cancelButtonIndex = 1;
[alert addButtonWithTitle:button2Title];
[alert show];
to no avail. Is it even possible to accomplish what I am trying to do?
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert show];
Might Help,
Cheers.
I have two ancillary points to this answer.
1) While, to the best of my knowledge, Apple has not rejected an app for reasonable modification of a UIAlertView; They have said that the view hierarchy of classes like UIAlertView should be considered private.
2) This question is a good example of why you should ask a question more about your end goal rather than the steps to get there. The only reason I know what this question is about is as a result of a comment left at my answer here.
Answer:
Because of your comment I know that you are looking to create a UIAlertView that has stacked buttons even when there are only 2 buttons.
I find the most logical place for code like this is in a category. Since generally the code needed to manipulate the alert-view needs to be around the show call, I created a category method I call instead of show and the method in turn calls show itself.
-(void)showWithButtonsStacked{
static NSString *tempButtonTitle = #"SomeUnlikelyToBeUsedTitle";
BOOL willAddFakeButton = (self.numberOfButtons == 2); // Button are only side by side when there's 2
if (willAddFakeButton){
self.clipsToBounds = YES;
[self addButtonWithTitle:tempButtonTitle]; // add temp button so the alertview will stack
}
BOOL hasCancelButton = (self.cancelButtonIndex != -1); // If there is a cancel button we don't want to cut it off
[self show];
if (willAddFakeButton){
UIButton *cancelButton = nil;
UIButton *tempButton = nil;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (hasCancelButton && [button.titleLabel.text isEqualToString:[self buttonTitleAtIndex:self.cancelButtonIndex]]){
cancelButton = button;
} else if ([button.titleLabel.text isEqualToString:tempButtonTitle]) {
tempButton = button;
}
}
}
if (hasCancelButton){ // move in cancel button
cancelButton.frame = tempButton.frame;
}
[tempButton removeFromSuperview];
// Find lowest button still visable.
CGRect lowestButtonFrame = CGRectZero;
for (UIButton *button in self.subviews) {
if ([button isKindOfClass:[UIButton class]]){
if (button.frame.origin.y > lowestButtonFrame.origin.y){
lowestButtonFrame = button.frame;
}
}
}
// determine new height of the alert view based on the lowest button frame
CGFloat newHeight = CGRectGetMaxY(lowestButtonFrame) + (lowestButtonFrame.origin.x * 1.5);
self.bounds = CGRectMake(0, 0, self.bounds.size.width, newHeight);
}
}
The way this method accomplishes it's goal is to add a temporary button to the alert-view to force the alert-view to stack the buttons, then it removes the temporary button and adjusts the height. Since it's a category method you use it simply by calling:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert showWithButtonsStacked];
This code results in an alert like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
[alert addButtonWithTitle:button1Title];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:button2Title];
[alert setCancelButtonIndex:1]; // to make it look like cancel button
[alert show];
Set the cancel button to nil and just add it in the other buttons instead

Disable Alertview Programmatically

I want to display a activity indicator in AlertView with no buttons, I want to disable it when activity indicator stops animating.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview:progress];
[progress startAnimating];
[alert show];
[progress release];
Then to dismiss the alert view and spinner: (you'll need to make the alert view a member)
[alert dismissWithClickedButtonIndex:0 animated:YES];
[alert release];
Just call dismissWithClickedButtonIndex:animated: method for UIAlertView
[alertView dismissWithClickedButtonIndex:0 animated:YES];
Just call the:
[theAlertViewInstance dismissWithClickedButtonIndex: 0 animated:YES];
This should do, what you need.

UIAlertView not displaying the keyboard

I have UIAlertView which is being displayed upon the load of a view.
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av becomeFirstResponder];
[av show];
However the keyboard is not being display on the iPad or the simulator?
I have also tried
I just tried
[av becomeFirstResponder];
and also
UITextField *text = [alertView textFieldAtIndex:0];
[text becomeFirstResponder];
I just tried this piece of code and it logs that the textField is the first responder but still no keyboard.
if([[av textFieldAtIndex:0] isFirstResponder] == YES){
NSLog(#"av is the first responder.");
}
Making the alert into the first responder won't help. You need to make te text box inside the alert view into the first responder.
Edit:
You may need to call reloadInputViews (with or without the s, don't remember). Also double check that you're not changing the input views anywhere that might be breaking them.
Edit 2:
You might want to move the alert from viewDidLoad into viewDidAppear. I've seen problems with UI elements being updated/presented too early. This is one of those cases, I think.
To use
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
av.tag=1;
av.alertViewStyle = UIAlertViewStyleSecureTextInput;
[av show];
and use this method.
- (void)alertView:(UIAlertView *)alertViews clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertViews.tag==1)
{
[textfieldname becomeFirstResponder];
}
}
this works
self.alertView = [[UIAlertView alloc]initWithTitle:#"Login" message:#"Please enter you mobile number" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
self.alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView show];
but not this
UIAlertView* alertView = [[UIAlertView alloc] init];
[alertView initWithTitle:...]
av = [[UIAlertView alloc]initWithTitle:#"New Password" message:#"please enter a new passward" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"done", nil];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform Transform = CGAffineTransformMakeTranslation(0, 60);
[tf setBackgroundColor:[UIColor whiteColor]];
[av setTransform:Transform];
[av addSubview:tf];
[av show];
This works but you should be able to do this in IOS 5 using UIAlertViewStyleSecureTextInput??
I had the same issue, but my solution looks like it might not apply to original poster...
I had this code :
UIAlertView* alert = [[UIAlertView alloc] init];
[alert initWithTitle:...]
When run, no keyboard appeared.
I changed it to this, and keyboard now appears:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:...];
Doing the init after alert was created seem to leave internal state of object as "this object doesn't need a keyboard!"

Adding sub-view inside an Other subview

I want to add an activity Indicator View inside and Alert Box is it possible to add an subView inside Another subView?
Use below code :
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#" " message:#" " delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *myActivityIndicatorView= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 60, 40, 40)];
myActivityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[myAlert addSubview:myActivityIndicatorView];
[myActivityIndicatorView startAnimating];
[myAlert show];
yes it is possible...
[alertView addSubview:activityIndicator];
Technically, yes.
Simply alloc and init both views, then call
[alertView addSubview:activityView];
However, if this passes Apple's inspection is doubtful, so you may not be able to publish to the AppStore.

Any non-private API alternative for this?

My app was recently rejected due to using a private API (addTextField: method for UIAlertView, which is quite useful, might I add).
Is there any non-private alternative to UIAlertView's undocumented addTextFieldWithValue:label:?
Thanks SO much in advance!
create the text field as a subview of the UIAlertView
// Ask for Username and password.
alertView = [[UIAlertView alloc] initWithTitle:_title message:#"\n \n \n" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
// Adds a username Field
utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
utextfield.placeholder = #"Username";
[utextfield setBackgroundColor:[UIColor whiteColor]];
utextfield.enablesReturnKeyAutomatically = YES;
[utextfield setReturnKeyType:UIReturnKeyDone];
[utextfield setDelegate:self];
[alertView addSubview:utextfield];
// Show alert on screen.
[alertView show];
[alertView release];
Check out this Open Source code on GitHub for adding UITextFields to a UIAlertView.
http://github.com/enormego/EGOTextFieldAlertView