iOS 8中的AlertView问题

时间:2015-09-21 05:27:14

标签: ios objective-c swift ios8 uialertview

我在AlertView中添加了一个UIVIew,如下所示:

java.io.StreamCorruptedException: invalid type code: 00
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    at hadoop.HadoopClient.readFileFromHDFS(HadoopClient.java:47)
    at hadoop.HadoopClient.retrieveByteArrayTimer(HadoopClient.java:159)
    at hadoop.HadoopClient.main(HadoopClient.java:114)

问题

UIView *alertSubView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 260, 55)];
        [alertSubView addSubview:textField];
        [alertSubView addSubview:charLeftLabel];

        [alert setValue:alertSubView forKey:@"accessoryView"];

当我尝试在Alertview委派方法中获取该子视图时,应用程序崩溃。

在iOS7中工作正常,但仅限iOS 8有问题

错误由于未捕获的异常终止应用' NSUnknownKeyException',原因:' [valueForUndefinedKey:]:此类不是密钥值编码兼容的关键附件

4 个答案:

答案 0 :(得分:4)

不推荐使用警报视图。您应该使用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

希望它对你有所帮助。

答案 1 :(得分:1)

你可以在 swift 2.0 中做同样的事情,如下面的代码:

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

答案 2 :(得分:0)

试试这段代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 55)];
[view addSubview:textField];
[view addSubview:charLeftLabel];
[alert setValue:view forKey:@"accessoryView"];
[alert show];

答案 3 :(得分:0)

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 55)];
[view addSubview:textField];
[view addSubview:charLeftLabel];
[alert setValue:view forKey:@"accessoryView"];
[alert show];