can I put multiple text input boxes on the alert box in iOS?
The best solution is to write or use a custom alert view that has the text fields that you need. If you only need 1 text field (or username/password) you can still use UIAlertView by setting the alertViewStyle property.
There are custom alert views already written, such as this one.
Yes, that's possible.
Just add UITextField as subviews to the UIAlertView.
Set the tag for each UITextField, in order to retrieve the entered text later.
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)];
textField1.tag=0;
textField1.borderStyle=UITextBorderStyleRoundedRect;
textField1.delegate=self;
[alert addSubview:textField1];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 85.0, 260.0, 25.0)];
textField2.tag=1;
textField2.delegate=self;
textField2.borderStyle=UITextBorderStyleRoundedRect;
[alert addSubview:textField2];
[alert show];
PLEASE NOTE that this is bad practice as it relies on UIAlertView being a subclass of UIView. As pointed out by MusiGenesis below, it won't work from iOS 7 onwards.
Hope that helps.
if you want 2 text boxes, you can use login style, but set second item secureTextEntry to NO, refer to
http://forums.macrumors.com/threads/uialertviews-with-textfields.1355954/
Related
I have this code for showing alert view with two needed extra objects:
- (void)leaveCommentButtonPressed
{
UIAlertView *leaveCommentAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Leave comment", nil)
message:#""
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 33)];
[textField setBackgroundColor:[UIColor lightGrayColor]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 33, 100, 67)];
[textView setBackgroundColor:[UIColor darkGrayColor]];
[view addSubview:textField];
[view addSubview:textView];
CGFloat system_version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (system_version < 7.0) //For Backward compatibility
{
[leaveCommentAlert addSubview:view];
}
else
{
[leaveCommentAlert setValue:view forKey:#"accessoryView"];
}
[leaveCommentAlert show];
}
But my problem is that I can't calculate width of alertView to set width for my text view and text field.
Maybe there are some other answers how to achieve text field and text view. But my idea is to have UIView with appropriate size.
Hic sunt dracones
Subclassing Notes
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.
You should use a alert view replacement. There are numerous in the web, for example: CXAlertView, DLAlertView or SDAlertView
If you're just looking to use a default implementation of UIAlertView, the Apple Docs also state:
Optionally, an alert can contain one or two text fields, one of which can be a secure text-input field. You add text fields to an alert after it is created by setting its alertViewStyle property to one of the styles specified by the UIAlertViewStyle constants. The alert view styles can specify no text field (the default style), one plain text field, one secure text field (which displays a bullet character as each character is typed), or two text fields (one plain and one secure) to accommodate a login identifier and password
See Alert Views
But, as mentioned in a different post, UIAlertView is deprecated and replaced by UIAlertController. Fortunately it comes with addTextFieldWithConfigurationHandler: that allows you to do what you're looking for.
I would like to determine width of UIAlertView before I show it and after I setup it. I need that to put UIImageView into it and adjust its width to width of UIAlertView. Is there any way to do that?
It is not a very good idea to try modifying the UIAlertView's height/width or add a UIImageView to it as this might result in your app getting rejected by the App Store. However, ios-custom-alertview is a good alternative.
A sample code would be:
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
[alertView setContainerView:yourSubView];
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:#"Close", nil]];
[alertView show];
You can use this, ios-custom-alertview
https://github.com/wimagguc/ios-custom-alertview
I have an app that was built on Xcode 4.6 and iOS 6. I am now upgrading it to iOS 7 using XCode 5.
I have this code that worked perfectly on iOS 6. What it does it bring a popup with UITextField in it. User can enter whatever text they want and hit OK. I take their text and put it in a UILabel.
The problem I have is that in iOS 7 when I bring up this pop-up text box, its not editable. Touching it doesn't do anything. How come?
Here is the code and screenshot
// ************
// ENTER TEXT
// ************
-(IBAction)insertText
{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Text \n"
message:#"\n\n Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
[dialog show];
}
Use the provided API instead this hack. Instead of adding your own text field (which was never actually supported), set the alert view's alertViewStyle to UIAlertViewStylePlainTextInput. This will give you a supported text entry field in the alert view.
I'm trying to create an UIAlertView with 3 TextFields and 3 Labels.
But with my current code they just go over each other, I've tried to change the frame of the AlertView and I've tried to use the CGAffineTransform but they both didn't work.
Here's my code:
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Sign in" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Stop" otherButtonTitles:#"Sign in",nil];
UILabel *customerIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 55, 65, 21)];
customerIdLabel.tag =1;
customerIdLabel.text = #"Customer id: ";
[myAlertView customerIdLabel];
UITextField *customerIdField = [[UITextField alloc] initWithFrame:CGRectMake(89, 50, 160, 30)];
customerIdField.tag = 2;
customerIdField.placeholder = #"Fill in your customer id";
[myAlertView customerIdField];
And then there's two more combinations of those.
Here's the transform that I've tried:
CGAffineTransform myTransform = CGAffineTransformMakeScale(1.0, 0.5f);
[myAlertView setTransform:myTransform];
And this is what I get:
I'm pretty sure you aren't supposed to use UIAlertViews like that. You're supposed to set the title, message, etc and use the [alertview show]. The documentation seems to be pretty clear that it is an as-is class.The UIAlertViewStyle may be something you can look at, but the options are 1 text field, 1 secure field, or a username and password field.
Your best option is to use a new UIViewController and use the presentViewController:animated:completion: call.
I get around this by not changing UIAlertView but add a clear view on top of it when it is showing (you can pad with spaces) and add the textfields there at the correct location (note when rotation occurs). Then, let the tap go through the view to the UIAlertView. This has gone through Apple in 3 apps in the store.
Before the new SDK in xcode 5 you where able to add subviews to an alert view but that doesn't work anymore. I had to update an app because of that nasty code. Is better to use uipopovercontroller for ipad or show a UIView or even present a viewcontroller for either ipad or iphone.
I have three buttons and a textview in uialertview, and tried to move down the buttons with add "\n" in uialertview's message property. But it's not work. The string will become "..." when it reach the limit of a line. the textview always cover my buttons. Do you have any suggestions? Sorry that I don't have the right to post image.
- (void)addMessage
{
self.addMessageAlertView = [[UIAlertView alloc] initWithTitle:#"Add Title"
message:#"\n\n\n\n function normal function normal function normal function normal function normal "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",#"Search", nil];
self.addMessageTextView = [[UITextView alloc] initWithFrame:CGRectMake(10.0, 75, 260.0, 25*2)];
[addMessageTextView setBackgroundColor:[UIColor whiteColor]];
addMessageTextView.font = [UIFont systemFontOfSize:20];
addMessageTextView.delegate=self;
addMessageTextView.layer.masksToBounds=YES;
addMessageTextView.layer.cornerRadius=10.0;
addMessageTextView.layer.borderWidth=0.0;
[addMessageAlertView addSubview:addMessageTextView];
[addMessageAlertView show];
[addMessageAlertView release];
[addMessageTextView release];
}
- (void)willPresentAlertView:(UIAlertView *)openURLAlert
{
[openURLAlert setFrame:CGRectMake( 10, 60, 300, 300 )];
[openURLAlert setBounds:CGRectMake(0, 10, 290, 290 )];
}
UIAlert view has maximum height (I've tried this same thing with no success); you won't be able to make it bigger. I suggest that you use a custom pop-over instead. A popover can act like an alert but will give you more flexibility.
This question has some links to tutorials: Are there examples of how to use UIPopoverController on iOS?