Textfield in alertview not working in ios7? - iphone

I have an application in which I'm using a specific design for a reason. I put a text field in an alert view above an otherbutton with a background image. Everything is working fine in ios 6 version.
UIAlertView *av=[[UIAlertView alloc] initWithTitle:#"fdhdj" message:#" hdfjkhfjkhdk" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#" ",#"cancel",nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
namefield = [[UITextField alloc] initWithFrame:CGRectMake(10.0,43.0, 264.0, 44.0)];
namefield.borderStyle = UITextBorderStyleNone;
namefield.background = [UIImage imageNamed:#"text_field_default.png"];
namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
namefield.textAlignment = UITextAlignmentCenter;
//[namefield setBackgroundColor:[UIColor whiteColor]];
[av addSubview:namefield];
[namefield release];
av.tag=12;
av.delegate=self;
[av show];
[av release];
But now in ios 7, I heard you can't easily alter the view hierarchy of a UIAlertView.
One alternative for this case is to set
alert.alertViewStyle = UIAlertViewStylePlainTextInput
But can we add that text field in wherever we want? As in my case above the first otherbutton.can anybody help me?

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter Student Name" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
i used to do like this ,and its working very fine

This is my component to support addSubview with alertView in iOS7.
CXAlertView - Custom alert-view which allow you to add view as main content.

The simple answer to your question is NO, you can't change anything in this testField for the UIAlertViewStylePlainTextInput and you shouldn't.
This is from Apple:
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.
And unfortunately what you heard I heard you can't easily alter the view hierarchy of a UIAlertView is wrong, you cannot alter the view hierarchy of a UIAlertView in iOS7 at all.
There are a good alternative on the web, you can check in cocoacontrols.com

You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.

Related

AutoCapitalization of TextField of UIAlertView

I’m popping up a UIAlertView with a UITextField in it. I want the text field to auto-capitalize all words. I’m doing this by setting properties of the text field, but they have no effect at runtime. Here’s my code:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: title
message: message
delegate: self
cancelButtonTitle: #"Cancel"
otherButtonTitles: #"Create", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* titleField = [alert textFieldAtIndex: 0];
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[alert show];
this autocapitalization is working on UITextField form XIB but this is not working on UIAlertView ,i am using iOS 6.
If it doesn't change your requirements, using
titleField.autocorrectionType = UITextAutocorrectionTypeDefault;
along with
titleField.autocapitalizationType = UITextAutocapitalizationTypeWords;
gives you the desired effect.
Make sure "Auto-Capitalization" is enabled in Settings in the iOS Simulator and/or on your test device.
Settings > General > Keyboard > Auto-Capitalization
If disabled, it will ignore the autocapitalizationType property on your UIAlertView's text field.
You can use uitextfield delegates
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
textField.text=[textField.text uppercase];
}

UIAlertView displays textboxes and buttons

I used the following code to create a UIAlertView and add some components on it but the result is at the image :(( (image is here : http://i.stack.imgur.com/DTg02.png)
-(IBAction)login:(id)sender
{
UIAlertView *login = [[UIAlertView alloc]initWithTitle: #"Login first"
message:#"enter username and password please first" delegate:self cancelButtonTitle:#"cancel"otherButtonTitles:#"OK", nil];
k = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 70.0, 200.0, 25.0)]; //<< it also displays wrong without this line
k.text = #"";
k.backgroundColor = [UIColor whiteColor];
k.clearButtonMode= UITextFieldViewModeWhileEditing;
k.keyboardType = UIKeyboardTypeAlphabet;
k.keyboardAppearance = UIKeyboardAppearanceAlert;
p = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 100.0, 200.0, 25.0)];
p.text = #"";
p.backgroundColor = [UIColor whiteColor];
p.clearButtonMode = UITextFieldViewModeWhileEditing;
p.keyboardType = UIKeyboardTypeAlphabet;
p.keyboardAppearance = UIKeyboardAppearanceAlert;
[login addSubview:k];
[login addSubview:p];
[login show];
}
if you want to add multiple textField and Multiple Button on UIAlertView then follow the step :
step 1 :
#interface ViewController ()<UIAlertViewDelegate>
{
UITextField *textFieldOne,*textFieldTwo,*textFieldThird;
}
step 2 :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Multiple TextField" delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert addButtonWithTitle:#"CANCEL"];
alert.delegate=self;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
textFieldOne=[[UITextField alloc]initWithFrame:CGRectMake(5, 0, 150, 20)];
[textFieldOne setPlaceholder:#"First Name"];
[myView addSubview:textFieldOne];
textFieldTwo=[[UITextField alloc]initWithFrame:CGRectMake(5,25, 150, 20)];
[textFieldTwo setPlaceholder:#"Middle Name"];
[myView addSubview:textFieldTwo];
textFieldThird=[[UITextField alloc]initWithFrame:CGRectMake(5,50, 150, 20)];
[textFieldThird setPlaceholder:#"Last Name"];
[myView addSubview:textFieldThird];
[alert setValue:myView forKey:#"accessoryView"];
[alert show];
step 3 :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"YES"])
{
NSLog(#"Button YES");
NSLog(#"TextFiledOne Text = %#",textFieldOne.text);
NSLog(#"TextFiledTwo Text = %#",textFieldTwo.text);
NSLog(#"TextFiledThree Text = %#",textFieldThird.text);
}
else if([title isEqualToString:#"NO"])
{
NSLog(#"Button NO");
}
else
{
NSLog(#"Cancel is click");
}
}
A UIAlertView is no thought for additional text input. You could use instead a UIActionSheet as it is explained here.
There is no point adding to the code that has already been added on here as it is good enough already and there are some good alternatives given. What I am going to do is tell you why you shouldn't be adding your own UITextFields to a UIAlertView using addSubview:.
Basically Apple have made the UIAlertView class to be used as is and the view hierarchy for this class is private.
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.
Subclassing note taken from UIAlertView Apple documentation.
In essence what this means is that if you use the addSubview: method on a UIAlertView then you are in essence altering something that Apple as indicated as being private and your app would get rejected from the Apple App Review Process for rule 2.5.
2.5 Apps that use non-public APIs will be rejected
You might be asking yourself why does that method even exist then for UIAlertView surely because its there we can use it. Well NO the reason it is there is because the UIAlertView class is itself is a subclass of UIView which is where the addSubview: method is declared. Unfortunately there is no why to stop a instance of UIAlertView from actually calling that method. So what Apple have done is they have overridden the addSubview: method and all it does is it returns. So this method makes it do nothing and any views you pass to this method will never get added, as it never calls [super addSubview:view];.
So when it comes to UIAlertViews there are two things that you shouldn't be doing and these are:
Subclass UIAlertView like #interface MYAlertView : UIAlertView this is subclassing and is not allowed.
Alter View Hierarchy like [alertView addSubview:view];
However there is one point I should make, whilst we aren't allowed to subclass UIAlertView we are still allowed to make categories for it like #interface UIAlertView (MyCategory) as this is not classed as subclassing it is known as a class category or class extension (I've also heard it called a class provider before).
It should also be noted that if you are developing for iOS8 and above you should be using UIAlertController as the UIAlertView has been deprecated.
Important:
UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
So I'd either use one of the custom classes mentioned in the other answers or move to use UIAlertController or if you have the time make your own custom alert view.
I hope this explanation helps you understand UIAlertViews more.
Don't go for all the bluffs. Just use the following code:
[login setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
And to handle the inputs of the user, use this:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([[alertView textFieldAtIndex:0].text isEqual:#"UserName"]&&[[alertView textFieldAtIndex:1].text isEqual:#"PassWord"])
{
//do your stuff here
[adminLoginButton setTitle:#"Logout" forState:UIControlStateNormal];
[adminLoginButton setImage:[UIImage imageNamed:#"Logout.png"] forState:UIControlStateNormal];
}
else
{
UIAlertView *errorMessage = [[UIAlertView alloc]initWithTitle:#"Authentication Error" message:#"Input is wrong" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles: nil];
[errorMessage show];
}
}

UITextField inside an UIAlertView not working in iOS4.1 and works in 3.0

I have a UITextField added to an UIAlerView (Kind of adding user to list with nick name, and nickname is asked by the alertview.). Thats work perfect in iOS3, But I cant type to that UITextField in iOS4.1. in iOS4.1 I do get focus to that textfield inside alertview, and keyboard appears, however typing doesn't work.
Please help in this regard.
In iOS 4 UIAlertViews will automatically be moved and sized to avoid the keyboard if they contain any UITextFields in their subviews so you don't have to move it yourself. Simply add a text field like so:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello"
message:#"Tap below to enter text:\n\n"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 68, 260, 30)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[alert addSubview:textField];
[textField release];
[alert show];
[alert release];
Be sure to add \n's to make room for your text field, you'll have to play around with getting the field in the correct position.
iOS4 broke all sorts of UIAlertView hacks (er... 'customizations').
I have a custom UIAlertView replacement class on github which supports an input (with UITextField) mode. You're welcome to try it out.
https://github.com/TomSwift/TSAlertView
I have created a post in my blog on the topic "How to add UITextField to UIAlertView from XIB".
To Solve your problem, you need to add a "fake" UITextField into the Alert view via coding. Please refer to the following link:
http://creiapp.blogspot.com/2011/08/how-to-add-uitextfield-to-uialertview.html

UIAlertViews disappear when calling UIActivityIndicatorView method

if I show an Alert and then call a method of an Activity Indicator, the alert disappears like the user has pressed OK.
Declaration of alert:
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
Declaration of Activity Indicator:
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.hidesWhenStopped = YES;
[indicator stopAnimating];
Method call which causes this problem:
[indicator stopAnimating];
What can I do?
Edit: I noticed that my app isn't working correct, if NSXML Parser is getting a wrong URL. At the moment I cannot say if the activity indicator is causing a error.
you could have the ActivityIndicator embedded in your alertview, looks neat and tidy:
http://kwigbo.com/post/394670170/iphone-sdk-uialertview-with-activity-indicator
Edit: Re-read your question, above solution might not be suitable for your needs.
Let me ask, what exactly are you trying to do with those two views?

How to add UITableView to UIAlertView?

I create a class:
#interface myUITableViewController : UIViewController
{
NSArray *listData;
}
...
and later,I do so:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
myUITableViewController *myUITable = [[myUITableViewController alloc] init];
[alert addSubview:myUITable.view];
[alert show];
After running,the result is that the myUITable.view's size is bigger than alert.
Why?
Please tell me if you know.
Thank you!
UIAlert is not really meant to be used like that. You should make your own custom UIView and add whatever content you need in it (the table and buttons). Then handle how it shows and hide yourself.
Even if you manage to get it shown correctly chances are it might break in the future. In one of my apps i was showing an alert with a UITextField. I was making space for it by adding "\n" to the message. In later iOS versions this stopped working and it looked really awful...