将UIDatePicker添加到UIAlertView [英] Add UIDatePicker to UIAlertView

查看:88
本文介绍了将UIDatePicker添加到UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期选择器添加到警报视图中。我可以根据按钮点击将动画添加到屏幕上,我看到一个黑盒但没有日期选择器。



这是我到目前为止所拥有的....

   - (IBAction)showDatePicker {
CGRect frame = CGRectMake(200,self.view.frame.size.height ,self.view.frame.size.width,0); // CGRectMake(225,145,260,125);
UIPickerView * datePicker = [[UIPickerView alloc] initWithFrame:frame];
UIAlertView * showDateAlert = [[UIAlertView alloc]
initWithTitle:@输入代码日期
消息:@示例消息
delegate:self
cancelButtonTitle: @取消
otherButtonTitles:@Update,nil];

[self.view addSubview:datePicker];
[UIView beginAnimations:@slideIncontext:nil];
[datePicker setCenter:CGPointMake(datePicker.frame.origin.x,self.view.frame.size.height - datePicker.frame.size.height / 2)];
[UIView commitAnimations];
}

我找到了一个看似有效的例子但我没有看到任何警报框中的对话框或按钮,只是日期选择器本身。我在这里遗漏了什么吗?

   - (IBAction)showDatePicker {
CGRect frame = CGRectMake(225,self.view .frame.size.height,self.view.frame.size.width,125);
UIDatePicker * datePicker = [[UIDatePicker alloc] initWithFrame:frame];
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;

[datePicker setDate:[NSDate date]];

UIAlertView * alert;
alert = [[UIAlertView alloc]
initWithTitle:@输入代码日期
消息:@示例消息
delegate:self
cancelButtonTitle:@取消
otherButtonTitles:@Update,nil];
alert.delegate = self;
[alert addSubview:datePicker];
[alert show];
}


解决方案

我同意评论者的意见一直说这不是实现这个目标的方法,原因有两个。首先,没有人喜欢不断出现警报视图。其次,并不适用于您的情况,最终可能会导致应用被拒绝。 Apple更改了 UIAlertView 类引用中的措辞,引用其视图层次结构为私有;我们都知道苹果对你认为是私有的感觉如何。


来自UIAlertView类参考:



UIAlertView类旨在按原样使用,而不是
支持子类化。此类的视图层次结构是私有的,并且不得修改


但是因为你说这是一个私人应用程序, 开始。 UIAlertView 只是一个 UIView 子类。所以帧和边界的所有规则仍然适用。以下是如何调整选择器框架和警报范围以挤入日期选择器的基本示例。

  / /创建警报
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Hello消息:@messagedelegate:self cancelButtonTitle:@CancelotherButtonTitles:@OK,nil];
//显示提醒(尺码要求可用)
[提示显示];
//创建日期选择器(可能/应该是ivar)
UIDatePicker * picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10,alert.bounds.size.height,320,216)];
//添加选择器以提醒
[alert addSubview:picker];
//调整警报范围
alert.bounds = CGRectMake(0,0,320 + 20,alert.bounds.size.height + 216 + 20);

编辑



<正如已经注意到许多我如何向 UIAlertView 问题添加一些视图的评论和新答案,此方法在iOS7中不再适用以上。这应该被视为这是一个根本不好的想法的证据。



还要注意 setValue:forKey:在一般流通的解决方案中对问题的使用无法修改Apple的私有视图层次结构。 setValue:forKey:是引起私有API扫描程序兴趣的方法之一。在 UIAlertView 文档和标题中搜索 accessoryView 。它不在文档中。标题中唯一相关的项目是名为 _accessoryView 的ivar,标记为 @private



再次对于内部或私人应用程序,我认为没关系,但是没关系。


I am trying to add a date picker to an alert view. I can add the animation to the screen based on a button click and I see a black box but no date picker.

Here is what I have so far....

- (IBAction)showDatePicker {
    CGRect frame = CGRectMake(200, self.view.frame.size.height, self.view.frame.size.width, 0);  //CGRectMake(225, 145, 260, 125);
    UIPickerView *datePicker = [[UIPickerView alloc] initWithFrame:frame];
    UIAlertView *showDateAlert = [[UIAlertView alloc]
                                  initWithTitle:@"Enter the Code Date"
                                  message:@"Sample message"
                                  delegate:self
                                  cancelButtonTitle:@"Cancel"
                                  otherButtonTitles:@"Update", nil];

    [self.view addSubview:datePicker];
    [UIView beginAnimations:@"slideIn" context:nil];
    [datePicker setCenter:CGPointMake(datePicker.frame.origin.x, self.view.frame.size.height - datePicker.frame.size.height/2)];
    [UIView commitAnimations];
}

I found an example that appears to be working BUT I don't see any dialog or buttons in my alert box, just the date picker itself. Am I missing something here?

- (IBAction)showDatePicker {
    CGRect frame = CGRectMake(225, self.view.frame.size.height, self.view.frame.size.width, 125);
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame];
    datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;

    [datePicker setDate:[NSDate date]];

    UIAlertView *alert;
    alert = [[UIAlertView alloc]
             initWithTitle:@"Enter the Code Date"
             message:@"Sample message"
             delegate:self
             cancelButtonTitle:@"Cancel"
             otherButtonTitles:@"Update", nil];
    alert.delegate = self;
    [alert addSubview:datePicker];
    [alert show];
}

解决方案

I agree with the commenters who have been saying this is not the way to accomplish this, for a couple reasons. First, no-one likes having alert-views constantly popping up. Second, and not applicable to your situation, It might eventually cause an app to be rejected. Apple has changed the wording in the UIAlertView class reference referring to its view hierarchy as private; And we all know how apple feels about you mucking about in what they consider private.

From UIAlertView class reference:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

But since you say this is a private app, here goes. A UIAlertView is just a UIView subclass. So all of the rules of frames and bounds still apply. Here's a basic sample of how to adjust the frame of the picker and bounds of the alert to squeeze in the date picker.

// Create alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
// Show alert (required for sizes to be available)
[alert show];
// Create date picker (could / should be an ivar)
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)];
// Add picker to alert
[alert addSubview:picker];
// Adjust the alerts bounds
alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20);

EDIT

As has been noted by both comments and new answers to many of the 'how do I add some view to a UIAlertView' questions, this method no longer works in iOS7 and above. This should really be taken as evidence that this is a fundamentally bad idea.

Also note the usage of setValue:forKey: in the generally circulated "solution" to the "problem" of not being able to modify Apple's private view hierarchy. setValue:forKey: is one of those methods that draws the interest of the private API scanners. Go search for accessoryView in the UIAlertView documentation and headers. It's not in the docs. The only related item in the headers is an ivar named _accessoryView which is marked as @private.

Again for an in-house or private app I suppose it's okay, just okay though.

这篇关于将UIDatePicker添加到UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆