0

次のコードを使用して を作成し、UIAlertViewその上にいくつかのコンポーネントを追加しましたが、結果は画像にあります :(( (画像はこちら: 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];
}
4

4 に答える 4

2

UIAlertView に複数の textField と複数のボタンを追加する場合は、次の手順に従います。

ステップ1 :

@interface ViewController ()<UIAlertViewDelegate>
{
    UITextField *textFieldOne,*textFieldTwo,*textFieldThird;
} 

ステップ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];

ステップ 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");
    }
}
于 2015-06-11T06:14:57.093 に答える
0

すべてのブラフに行かないでください。次のコードを使用してください。

[login setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

ユーザーの入力を処理するには、これを使用します。

-(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];
    }
}
于 2013-08-06T09:00:29.283 に答える
0

AUIAlertView追加文字入力は考えていません。hereUIActionSheetで説明されているように、代わりに a を使用できます。

于 2013-04-20T14:10:16.017 に答える