Objective c 无法将UITextField添加到iOS 7上的UIAlertView…在iOS 6中工作

Objective c 无法将UITextField添加到iOS 7上的UIAlertView…在iOS 6中工作,objective-c,ios7,uitextfield,uialertview,uialertviewdelegate,Objective C,Ios7,Uitextfield,Uialertview,Uialertviewdelegate,下面的代码在iOS6(和之前的版本)上运行,但UITextField在iOS7中不显示……关于如何在iOS7的UIAlterView中显示UITextField,您有什么想法吗 UIAlertView* dialog = [[UIAlertView alloc] init]; [dialog setDelegate:self]; [dialog setTitle:@"Enter ESC Score"]; [dialog setMessage:@" "]; [dialog addButtonWit

下面的代码在iOS6(和之前的版本)上运行,但UITextField在iOS7中不显示……关于如何在iOS7的UIAlterView中显示UITextField,您有什么想法吗

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setKeyboardType:UIKeyboardTypeNumberPad];
[nameField becomeFirstResponder];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];

[nameField release];
针对iOS6运行的代码显示以下内容:

iOS7中的相同代码显示了这一点(注意UITextField是如何丢失的,并且没有键盘):


在iOS 7中,您无法轻松更改UIAlertView的视图层次结构。(您也不应该这样做;文档明确告诉您不要这样做。)前往开发人员论坛,查看关于它的长时间讨论


在您的情况下,另一种选择是设置
alert.alertViewStyle=UIAlertViewStylePlainTextInput这将为您添加一个文本字段。您可以使用
UITextField*textField=[alertView textfieldatinex:0]在UIAlertView委托回调中访问它

@Aaron Brager找到了正确的解决方案。此外,我在他的建议后添加了一行,默认使用数字键盘

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter ESC Score"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
dialog.tag = 5;

dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog textFieldAtIndex:0].keyboardType = UIKeyboardTypeNumberPad;

CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 0.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
1) 方法 -(id)initWithAlertTitle:(NSString*)密码的标题检查:(NSString*)密码
你应该加上

self.alertViewStyle = UIAlertViewStylePlainTextInput;
样本:

(id)initWithAlertTitle:(NSString *)title
        checkForPassword:(NSString *)password{
     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {
    self.alertViewStyle = UIAlertViewStylePlainTextInput;
    }
    self = [super initWithTitle:title
                        message:@"" // password field will go here
                       delegate:self
              cancelButtonTitle:@"Cancel"
              otherButtonTitles:@"Enter", nil];
    if (self) {
        self.password = password;
        self.hashTechnique = HashTechniqueNone; // use no hashing by default
        secondMessage = @"Please Enter New Password";
        thirdMessage = @"Please Re-Enter Password";
        secondMessageNew = @"Please Enter Password";
    }

NSLog(@" _password_ %@",_password);
NSLog(@"_old_password_ %@",[[NSUserDefaults standardUserDefaults] objectForKey:kPassword]);

return self;
}
在方法展示中 添加下一个

(void)show {

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
     {

       UITextField *passwordField = [self textFieldAtIndex:0];
       passwordField.delegate = self;
      self.passwordField = passwordField;
    }
   else
   {
               UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(14, 45, 256, 25)];
               passwordField.secureTextEntry = YES;
               passwordField.placeholder = @"";
               passwordField.backgroundColor = [UIColor whiteColor];



               // Pad out the left side of the view to properly inset the text
               UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 19)];
                passwordField.leftView = paddingView;
                passwordField.leftViewMode = UITextFieldViewModeAlways;

           //    // Set delegate
           self.passwordField.delegate = self;

           // Set as property

            self.passwordField = passwordField;
           // Add to subview
           [self addSubview:_passwordField];
     }

    // Show alert
   [super show];

}
还可以在方法单击中进行更改

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {


    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        UITextField *passwordField = [self textFieldAtIndex:0];
        self.passwordField = passwordField;
    }

    if (buttonIndex == alertView.firstOtherButtonIndex) {

        if ([self enteredTextIsCorrect] || [self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) {

            if (([self.title isEqualToString:secondMessage] || [self.title isEqualToString:secondMessageNew]) && (self.passwordField.text.length > 0)) {
                self.password = self.passwordField.text;
                self.title = thirdMessage;
                self.passwordField.text = @"";

                if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                {
                    if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                        [self.passwordDelegate notifyParent:thirdMessage:self.password];
                    }
               }
            }else
            {
                if ([self.title isEqualToString:thirdMessage]) {
                    [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:kPassword];
                    [[NSUserDefaults standardUserDefaults] synchronize];

                    if (self.passwordDelegate) {
                        if ([self.passwordDelegate respondsToSelector:@selector(notifyParentWithState:)]) {
                            [self.passwordDelegate notifyParentWithState:YES];
                        }
                    }
                }else{
                    if ([self.title isEqualToString:secondMessageNew]) {
                        self.title = secondMessageNew;
                    }
                    else{
                        self.title = secondMessage;
                    }

                     self.passwordField.text = @"";
                     if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
                     {
                         if ([self.passwordDelegate respondsToSelector:@selector(notifyParent::)]) {
                             [self.passwordDelegate notifyParent:self.title:self.password];
                        }
                    }
                }
            }
        }

        // If incorrect then animate
        else {
            [self animateIncorrectPassword];
        }
    }
}

您还可以在cocoacontrols.com上查看自定义控件。看看(类似ios 7的用户界面)和(类似ios 6的用户界面)。它们也可以被转换成旋转角度。

就像魔咒一样

所有版本iOS的UIAlertView中有两个UIExtField

-(IBAction) showAlertView {

    UIAlertView *alert;  
    UITextField *callForwardNumber;
    UItextField *callForwardCondition;

    alert = [[UIAlertView alloc] initWithTitle:@"Enter Phone Number & Rule"
                                   message:@""
                                  delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Save", nil];

    //alert.transform = CGAffineTransformMakeTranslation(0, 110);

    callForwardNumber = [[UITextField alloc] init];
    callForwardNumber.keyboardType = UIKeyboardTypeNumberPad;
    callForwardNumber.text = [R.prefs objectForKey:@"fwd_number"];
    callForwardNumber.borderStyle = UITextBorderStyleRoundedRect;
    callForwardNumber.delegate = self;
    callForwardNumber.tag = 1;

    callForwardCondition = [[UITextField alloc] init];
    callForwardCondition.text = callCondition;
    callForwardCondition.borderStyle = UITextBorderStyleRoundedRect;
    callForwardCondition.delegate = self;
    callForwardCondition.tag = 2;
    [callForwardCondition setKeyboardType:UIKeyboardTypeNumberPad];

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    UIView* customAccessory = 
                   [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 55)];
    callForwardNumber.frame = CGRectMake(0, 0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(0, 30.0, 245.0, 25.0);
    [customAccessory addSubview:callForwardNumber];
    [customAccessory addSubview:callForwardCondition];
    [alert setValue:customAccessory forKey:@"accessoryView"];
    [alert show];
} else {
    alert.message = @"\n\n\n";
    [alert show];
    callForwardNumber.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
    callForwardCondition.frame = CGRectMake(20.0, 75.0, 245.0, 25.0);
    [alert addSubview:callForwardNumber];
    [alert addSubview:callForwardCondition];
}

}

我也面临着同样的问题,在冲浪的过程中我得到了答案,这对我来说很有效。我希望它对你也有用

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Folder Name?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    {
    UITextField * alertTextField = [alertView textFieldAtIndex:0];
    NSLog(@"alerttextfiled - %@",alertTextField.text);
}

iOS7仍然是测试版,你到底期望什么呢?嗯……我想如果苹果将在9月中旬发布iOS7,我希望能够与一个有才华、聪明、志同道合的开发者社区合作,这样我们的应用程序就不会对成千上万的现有苹果用户无法使用。苹果不希望任何人谈论如何主动让用户满意,这似乎有点傻。那么,我们是否要假装iOS7在正式发布之前并不存在?NDA很重要,但这似乎很愚蠢……甚至更愚蠢的是,有人花时间来回答人们提出的iOS7问题和扣分。只是说……苹果提供了一个讨论iOS7的论坛,在这个论坛上可以讨论被禁止的API。这里关于未发布、可能无法运行的软件的讨论对其他人来说并没有太大用处——除非特别标注了版本号难以置信,还必须为ios 7安装全新的osx,难以置信!猜猜这一问题在iOS 7的公开发行版中出现了什么,我们的应用程序在这方面也被破坏了。任何使用这种技术的应用程序(其实并不少见)都会被破坏。我很抱歉,伙计们,回答“在保密协议下,对其他人没有用处”是完全错误的方法来维持一个富有成效的社区,它应该在现实世界的问题上相互帮助,这绝对是一个错误的方法。无论测试版与否,这些问题都是存在的——因此请对此保持建设性,而不是不屑一顾,也不要给问题增加任何价值。这对单个文本字段很有效。如果我想向uialertview添加两个文本字段,那么解决方案是什么?@loganathan
UIAlertViewStyleLogin和PasswordInput
。如果不想隐藏第二个框,您可以设置
[alertView textFieldAtIndex:1].secureTextEntry=NO
。对于任何比这更复杂的问题,请发表您自己的看法。@AaronBrager有意义:-)@AaronBrager,但它会被苹果应用程序评论接受吗?3个文本字段呢?我们将其用于密码重置功能:“当前密码、新密码、再次新密码”。遵循@Aaron Brager的建议并改变警报的配置方式。。。就像iTrout为我做的一样!谢谢你们两个!!!我的解决方案适用于iOS 5及以上版本(您仅在iOS 7及以上版本上使用)。除非你支持iOS 4,否则没有必要进行这种黑客攻击。此外,无论你从哪个应用程序复制了它,都会在NSUserDefaults中保存密码,这是一个糟糕的主意。
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Folder Name?" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.tag = 2;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    {
    UITextField * alertTextField = [alertView textFieldAtIndex:0];
    NSLog(@"alerttextfiled - %@",alertTextField.text);
}