UITertController与UITextfield不显示标题和消息

时间:2015-09-29 12:16:58

标签: ios objective-c ios8

请帮我解决以下问题。

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

我使用了上面的代码,但它在下面的截图中显示了我的结果。无法显示标题和消息
enter image description here

提前致谢!

5 个答案:

答案 0 :(得分:8)

这是创建消息和标题的创建UIAlertController的代码。

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

对于swift,你可以做同样的事情,如下面的代码:

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

从Github下载演示:https://github.com/nitingohel/NGAlertViewController-Swift2.0

答案 1 :(得分:2)

我测试了你在这里发布的代码,并且它在所有模拟器以及iOS 8.0+中的设备中都能正常工作或者如果你想在ios 7.0中测试它,那么你必须使用UIAlertview。

代码是:view 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

输出捕捉:

enter image description here

答案 2 :(得分:0)

坦白说,你的代码工作正常。只是预感,尝试添加[self presentViewController:alertController animated:YES completion:nil]; 主队列上的这段代码。

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

答案 3 :(得分:0)

我修改了你的代码,它对我有用。基本上,我用" show"控制器而不是"现在"。

以下是剪辑:

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

答案 4 :(得分:0)

尝试使用此代码。

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "ionic"
npm ERR! node v4.1.2
npm ERR! npm  v2.14.4

npm ERR! Hostname/IP doesn't match certificate's altnames: "Host: registry.npmjs.org. is not in the cert's altnames: DNS:a.sni.fastly.net, DNS:a.sni.global-ssl.fastly.net"
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     C:\xampp\htdocs\receipt_finder\npm-debug.log