64

Mã bên dưới hoạt động trên iOS6 (và trước đó) nhưng UITextField không hiển thị trong iOS7 ... bất kỳ ý tưởng nào về cách nhận UITextField để hiển thị trong một UIAlterView trong iOS7?Không thể thêm UITextField vào UIAlertView trên iOS7 ... hoạt động trong iOS 6

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]; 

Mã chạy cho iOS6 sẽ hiển thị này:

enter image description here

cùng mã trong iOS7 hiển thị này (chú ý cách UITextField là mất tích và không có bàn phím):

enter image description here

+1

iOS7 vẫn là _beta_, bạn mong đợi điều gì? – holex

+31

Vâng ... Tôi đoán nếu Apple sắp phát hành iOS7 vào giữa tháng 9, tôi hy vọng có thể làm việc với một cộng đồng các nhà phát triển tài năng, sáng sủa và có đầu óc để Ứng dụng của chúng tôi không thể sử dụng được cho hàng ngàn Apple hiện có người dùng. Có vẻ ngớ ngẩn đến nỗi Apple không mong đợi bất kỳ ai nói về cách chủ động trong việc giữ cho người dùng của họ hạnh phúc. Vậy chúng ta có giả vờ rằng iOS7 không tồn tại cho đến khi nó chính thức được phát hành không?NDA là quan trọng, nhưng điều này có vẻ ngớ ngẩn ... và thậm chí sillier rằng ai đó đang dành thời gian để đi qua các câu hỏi iOS7 và điểm ding từ người dân. Chỉ cần nói ... – iTrout

+2

Apple cung cấp một diễn đàn để thảo luận về iOS7 trong đó API bị cấm vận có thể được thảo luận. Các thảo luận ở đây về phần mềm chưa được phát hành và có thể không hoạt động không hữu ích cho người khác - trừ khi được gắn thẻ đặc biệt với số phiên bản – marko

Trả lời

178

Bạn không thể dễ dàng thay đổi phân cấp khung nhìn của UIAlertView trong iOS 7. (Bạn cũng không nên; tài liệu hướng dẫn cifically nói với bạn không.) Đi qua các diễn đàn phát triển để xem một cuộc thảo luận dài về nó.

Một giải pháp thay thế trong trường hợp của bạn là đặt alert.alertViewStyle = UIAlertViewStylePlainTextInput; Thao tác này sẽ thêm trường văn bản cho bạn. Bạn có thể truy cập nó trong cuộc gọi lại đại biểu UIAlertView bằng cách sử dụng UITextField *textField = [alertView textFieldAtIndex:0];.

+2

Điều này làm việc tốt cho một trường văn bản duy nhất. Trong trường hợp tôi muốn thêm hai trường văn bản vào uialertview thì giải pháp là gì? – loganathan

+9

@loganathan 'UIAlertViewStyleLoginAndPasswordInput'. Bạn có thể đặt '[alertView textFieldAtIndex: 1] .secureTextEntry = NO' nếu bạn không muốn che khuất hộp thứ hai. Đối với bất cứ điều gì phức tạp hơn này, làm cho quan điểm của riêng bạn. –

+0

@AaronBrager có ý nghĩa :-) – loganathan

17

@Aaron Brager có giải pháp đúng. Ngoài ra tôi đã thêm một dòng sau đề xuất của anh ấy để mặc định Bàn phím số.

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

Theo cả gợi ý của @Aaron Brager VÀ thay đổi cách Alert được cấu hình ... như iTrout đã thực hiện thủ thuật cho tôi! Cám ơn hai bạn!!! –

0

1) Trong phương pháp - (id) initWithAlertTitle: (NSString *) tiêu đề checkForPassword: (NSString *) mật khẩu
bạn nên thêm

self.alertViewStyle = UIAlertViewStylePlainTextInput; 

mẫu:

(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; 
} 

trong phương thức hiển thị thêm tiếp theo

(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]; 

} 

cũng thực hiện thay đổi trong phương pháp bấm

#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]; 
     } 
    } 
} 
+0

Giải pháp của tôi hoạt động trên iOS 5 trở lên (bạn chỉ sử dụng nó trên iOS 7 trở lên). Không cần thiết cho hackery này trừ khi bạn đang hỗ trợ iOS 4. Ngoài ra, bất cứ ứng dụng bạn sao chép này từ là tiết kiệm một mật khẩu trong NSUserDefaults, đó là một ý tưởng khủng khiếp. –

6
UIAlertView *alertView = [[UIAlertView alloc] 
            initWithTitle:@"Credit Card Number" 
            message:@"Please enter your credit card number:" 
            delegate:self 
            cancelButtonTitle:@"Cancel" 
            otherButtonTitles:@"Ok", nil]; 
     [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
     /* Display a numerical keypad for this text field */ 
     UITextField *textField = [alertView textFieldAtIndex:0]; 
     textField.keyboardType = UIKeyboardTypeNumberPad; 

[alertView show]; 
0

Bạn cũng có thể cung cấp cho một cái nhìn để điều khiển tùy chỉnh trên cocoacontrols.com. Hãy xem MLAertView (ios 7 như giao diện người dùng) và TSAlertView (ios 6 như giao diện người dùng). Chúng cũng có thể được chuyển thành góc quay.

1

Chỉ cần làm việc như quyến rũ

Hai UITextField trong UIAlertView cho tất cả các phiên bản của iOS

-(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]; 
} 

} 
0

Tôi cũng phải đối mặt với cùng một vấn đề, Trong lướt tôi có câu trả lời cho nó hoạt động cho tôi. Tôi hy vọng nó cũng làm việc cho bạn.

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); 
} 
Các vấn đề liên quan