2015-09-29 14 views
5

Hãy giúp tôi với vấn đề dưới đây.UIAlertController với UITextfield không hiển thị Tiêu đề và Thông báo

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
    textField.keyboardType = UIKeyboardTypeNumberPad; 
    textField.placeholder = @"What was the value collected?"; 
}]; 

[alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) 
{     
    UITextField *txtValue = alertController.textFields.firstObject;      
    toCollect = [txtValue.text floatValue]; 
}]]; 
[self presentViewController:alertController animated:YES completion:nil]; 

Tôi đã sử dụng mã ở trên nhưng nó cho tôi thấy kết quả dưới ảnh chụp màn hình. Không thể hiển thị tiêu đề và thông báo
enter image description here

Cảm ơn bạn trước!

+0

toCollect là gì? –

+0

biến nổi của nó để lưu đầu vào giá trị văn bản trong uitextfield. – BKjadav

+0

mã của bạn hoạt động tốt. –

Trả lời

8

Đây là mã để tạo UIAlertController để hiển thị Thông báo và tiêu đề.

UIAlertController *alertController = [UIAlertController 
             alertControllerWithTitle:@"Alert Controller" 
             message:@"Alert Message" 
             preferredStyle:UIAlertControllerStyleAlert]; 

UIViewController *viewController = [[UIViewController alloc]init]; 
[viewController.view setBackgroundColor:[UIColor blueColor]]; 

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 30)]; 
lbl.text = @"This is a label"; 
lbl.textAlignment = NSTextAlignmentCenter; 
lbl.textColor = [UIColor whiteColor]; 
[viewController.view addSubview:lbl]; 

UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 35, 250, 30)]; 
tf.borderStyle = UITextBorderStyleRoundedRect; 
tf.placeholder = @"Enter your name"; 
[viewController.view addSubview:tf]; 

[alertController setValue:viewController forKey:@"contentViewController"]; 

UIAlertAction *cancelAction = [UIAlertAction 
           actionWithTitle:@"Cancel" 
           style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction *action) 
           { 
            NSLog(@"Cancel action"); 
           }]; 

UIAlertAction *okAction = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction *action) 
          { 
           NSLog(@"OK action"); 

           NSLog(@"Text Value : %@",tf.text); 
          }]; 

[alertController addAction:cancelAction]; 
[alertController addAction:okAction]; 

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 

enter image description here

Đối nhanh chóng bạn có thể làm điều tương tự như đoạn mã sau:

let alert = UIAlertController(title: "Alert Controller", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 


let cancelAction = UIAlertAction(
    title: "Cancel", 
    style: UIAlertActionStyle.Destructive) { (action) in 

} 

let confirmAction = UIAlertAction(
    title: "OK", style: UIAlertActionStyle.Default) { (action) in 

} 

alert.addAction(confirmAction) 
alert.addAction(cancelAction) 

let VC = UIViewController() 
VC.view.backgroundColor = UIColor.blackColor() 


let lbl = UILabel() 
lbl.frame = CGRectMake(10, 8, 250, 30) 
lbl.text = "this is a label" 
lbl.textAlignment = NSTextAlignment.Center 
lbl.textColor = UIColor.whiteColor() 
VC.view .addSubview(lbl) 


let txt = UITextField() 
txt.frame = CGRectMake(10, 35, 250, 30) 
txt.borderStyle = UITextBorderStyle.RoundedRect 
txt.placeholder = "enter text" 
VC.view .addSubview(txt) 


alert.setValue(VC, forKey: "contentViewController") 

self.presentViewController(alert, animated: true, completion: nil) 

enter image description here

Tải Demo từ Github: https://github.com/nitingohel/NGAlertViewController-Swift2.0

+0

Cảm ơn bạn đã làm việc cho tôi !!! – BKjadav

+0

bạn được chào đón và vui lòng giúp bạn. cám ơn –

0

Để thẳng thắn, mã của bạn hoạt động tốt. Chỉ cần một linh cảm, hãy thử thêm [self presentViewController:alertController animated:YES completion:nil]; đoạn mã này vào hàng đợi chính.

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self presentViewController:alertController animated:YES completion:nil]; 
}); 
+0

nó cho tôi cảnh báo - "Ứng dụng này đang sửa đổi động cơ autolayout từ một chủ đề nền, có thể dẫn đến hỏng động cơ và tai nạn kỳ lạ. Điều này sẽ gây ra một ngoại lệ trong một bản phát hành trong tương lai." – BKjadav

+0

xem câu trả lời đã chỉnh sửa – iAnurag

+1

Tôi không hiểu, tôi thấy nhiều câu hỏi với uialertcontroller với cùng một vấn đề. Dường như lỗi khủng khiếp trên iOS cho họ !!! –

0

Tôi đã sửa đổi mã của bạn một chút và nó hoạt động cho tôi. Về cơ bản, tôi sử dụng bộ điều khiển "hiển thị" thay vì "hiện tại".

Dưới đây là ảnh chụp:

-(IBAction)pressMe:(id)sender { 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
    { 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
     textField.placeholder = @"What was the value collected?"; 
    }]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

     UITextField *txtValue = alertController.textFields.firstObject; 

     NSString *string = txtValue.text; 
     NSLog(@"String: %@", string); 
    }]]; 
    [self showViewController:alertController sender:nil] 
} 
0

Hãy thử mã này cho điều đó.

if ([UIAlertController class]) 
    { 
     // use UIAlertController 
     UIAlertController *alert= [UIAlertController 
             alertControllerWithTitle:@"Enter Folder Name" 
             message:@"Keep it short and sweet" 
             preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action){ 
                 //Do Some action here 
                 UITextField *textField = alert.textFields[0]; 
                 NSLog(@"text was %@", textField.text); 

                }]; 
     UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                  NSLog(@"cancel btn"); 

                  [alert dismissViewControllerAnimated:YES completion:nil]; 

                 }]; 

     [alert addAction:ok]; 
     [alert addAction:cancel]; 

     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"folder name"; 
      textField.keyboardType = UIKeyboardTypeDefault; 
     }]; 

     [self presentViewController:alert animated:YES completion:nil]; 

    } 
2

tôi đã được thử nghiệm mã của bạn bạn đã đăng ở đây và thats làm việc tốt trong tất cả các giả lập cũng như các thiết bị cũng trong iOS 8.0+ HOẶC nếu bạn muốn kiểm tra nó trong ios 7.0 thì bạn phải sử dụng UIAlertview.

Mã là: xem controller.m

@interface ViewController() 
{ 
    float toCollect; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(IBAction)btntapped:(id)sender 
{ 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; 
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
    { 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
     textField.placeholder = @"What was the value collected?"; 
    }]; 

    [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

     UITextField *txtValue = alertController.textFields.firstObject; 

     toCollect = [txtValue.text floatValue]; 
}]]; 
    [self presentViewController:alertController animated:YES completion:nil]; 
} 

@end 

Snaps của Output:

enter image description here

Các vấn đề liên quan