问题
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];
}
回答1:
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");
}
}
回答2:
A UIAlertView
is no thought for additional text input. You could use instead a UIActionSheet
as it is explained here.
回答3:
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 UITextField
s 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 UIAlertView
s 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 UIAlertView
s more.
回答4:
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];
}
}
来源:https://stackoverflow.com/questions/16119132/uialertview-displays-textboxes-and-buttons