Any non-private API alternative for this? - iphone

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

Related

Adding TextField to UIAlertView

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.

No Keyboard with UIAlertViewStylePlainTextInput ?

I have a problem with the UIAlertViewStylePlainTextInput. I get my Alert with the textfield but there is no Keyboard coming up..? Here is my code:
UIAlertView *enter = [[UIAlertView alloc] initWithTitle:#"Highscores" message:#"Please Enter Your Name" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:#"Abbrechen" nil];
enter.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
[enter show];
I have tried without the
UITextField *theTextField = [enter textFieldAtIndex:0];
theTextField.placeholder = #"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;
part but still there is no keyboard coming up.
(I've tested it on my device and on the simulator)
Do you have any ideas about that?
thank you in advance.
I had the problem because I was triggering the alert when a UITextField delegate method textFieldDidBeginEditing was called in my view and then I would fill this with the content of the Alert view text field. That meant that the UITextField in my view became first responder and I couldn't find a way to resign. So if you have the problem it is because something else is becoming first responder before the UIAlert is displayed. Think about how you trigger the alert.
I resigned to using a tap gesture on my UITextField which then triggers the UIAlertView with UIAlertViewStylePlainTextInput and it works fine.
Some code below:
-(void)addTextFieldToView{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
textField.backgroundColor = [UIColor whiteColor];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textAlignment = UITextAlignmentCenter;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showAlertWithTextField:)];
[textField addGestureRecognizer:tapGestureRecognizer];
[self.view addSubview:textField];
}
-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK",nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
Try below code:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Your name?"
message:nil
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
Works on iOS5
Might fix your problem as it solved mine:
[enter resignFirstResponder];

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!"

Custom alertview showing on top?

I am facing a weird situation. I have a tab bar application,where i am showing a custom alertbox in a particular view.The problem is that the alertbox always display on top rather than middle of screen.
I am currently using Xcode 3.2.5 & build it on iPhone simulator 4.2
Edit
-(void)createAlertbox{
alertView = [[UIAlertView alloc] init];
[alertView setDelegate:self];
[alertView setTag:1];
[alertView setTitle:#"sample"];
[alertView setMessage:#" "];
[alertView addButtonWithTitle:#"Enter"];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0,60.0);
[alertView setTransform: moveUp];
ageTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0,25.0)];
[ageTextField setBackgroundColor:[UIColor whiteColor]];
[ageTextField setPlaceholder:#"Enter your Current Age"];
ageTextField.keyboardType=UIKeyboardTypeNumberPad;
ageTextField.delegate=self;
[alertView addSubview:ageTextField];
[alertView show];
[alertView release];
[ageTextField release];
}
I don't know why phix23 did't put that comment as an answer!
It's because of the transform :
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0,60.0);
[alertView setTransform: moveUp];
You're telling the ui alert view to be 60px higher than it would otherwise be. Delete these lines and I bet it's in the center again.
Where did you get your code from - I'm assuming that because you didn't know what the transform did you have cut-and-pasted it from somewhere?

TextField Covering UIAlertView's Button

I am using a UIAlertView with three buttons: "Dismiss", "Submit Score" and #"View Leaderboard". The UIAlertView also contains a UITextField called username. At the moment the UITextField "username" is covering one of the buttons in the UIAlertView. I just wanted to know how I could stop the UITextField from covering one of the buttons, i.e move the buttons down.
Here is an image of what is happening:
screenshot http://img684.imageshack.us/img684/3055/screenshot20110108at191.png
And here is my code:
[username setBackgroundColor:[UIColor whiteColor]];
[username setBorderStyle:UITextBorderStyleRoundRect];
username.backgroundColor = [UIColor clearColor];
username.returnKeyType = UIReturnKeyDone;
username.keyboardAppearance = UIKeyboardAppearanceAlert;
username.placeholder = #"Enter your name here";
username = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
username.borderStyle = UITextBorderStyleRoundedRect;
[username resignFirstResponder];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations"
message:[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n", tapAmount, originalCountdownTime]
delegate:self
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"Submit To High Score Leaderboard", #"View Leaderboard", nil];
alert.tag = 01;
[alert addSubview:username];
[alert show];
[alert release];
I think you need to add some new lines and a final space at the end of the message string.
[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n\n\n ", tapAmount, originalCountdownTime]
Here is a UIAlertView replacement class that supports user-input, custom width, and more:
https://github.com/TomSwift/TSAlertView
i had the same issue , your code has the following condition
[alert addSubview:username];
[alert show];
add the UITextfield after the [alert show]; as
[NSString stringWithFormat:#"You tapped %i times in %i seconds!\n\n\n ", tapAmount, originalCountdownTime];
[alert show];
[alert addSubview:username];
Thats all.working fine.
You shouldn't use a UIAlertView in this manner. It was never intended for this type of user interaction and view customization:
From the iOS Human Interface Guidelines:
You can specify the text of the required title and optional message, the number of buttons, and the button contents in an alert. You can’t customize the width or the background appearance of the alert view itself, or the alignment of the text (it’s center-aligned).
Perhaps consider building a custom view instead?