iOS4 创建两个 UIActionSheets,3.1.3 创建一个?为什么?
下面的代码在 XCode 3.2.2、iPhone OS 3.1.3 中创建一个 UIActionSheet。操作表中嵌入了一个日期选择器。
但它似乎在 XCode 3.2.3、iOS4 中创建了两张表,一张重叠在另一张上。
另外,我在控制台中收到此错误(同样,仅适用于 XCode 3.2.3、iOS4):
wait_fences: failed to receive reply: 10004003
您认为问题是什么?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField tag] >0 )
{
[self dismissKeyboard];
NSString *dateTitle;
dateFieldNumber = [textField tag];
dateTitle = @"Enter your Birth Date";
NSString *title = @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(dateTitle, @"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Ok", nil];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
UIDatePicker *datePicker2 = [[UIDatePicker alloc] init];
self.datePicker = datePicker2;
[datePicker2 release];
self.datePicker.date = [NSDate date];
[self.datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
self.datePicker.datePickerMode = UIDatePickerModeDate;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:60];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-60];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];
[calendar release];
[self.datePicker setMaximumDate:maxDate];
[self.datePicker setMinimumDate:minDate];
[actionSheet addSubview:self.datePicker];
return NO;
} else {
return YES;
}
}
编辑
正如 Brian 在下面指出的,textFieldShouldBeginEditing 委托方法被调用了两次。我不明白为什么在 IOS4 中会出现这种情况,而在早期版本中则不然。无论如何,我已经更改了代码,因此有一个 BOOL hasDisplayedSheet,它最初在 viewDidLoad 中为 NO。然后,我用 if 语句将操作表创建代码(在 textFieldShouldBeginEditing 委托方法中)括起来,就像
if (hasDisplayedSheet == NO) {
// actionsheet code
hasDisplayed = YES
}
当他们单击工作表按钮时将 hasDiplayedSheet
更改回 NO。
The code below creates one UIActionSheet in XCode 3.2.2, iPhone OS 3.1.3. Embedded in the Action Sheet is a date picker.
But it seems create two sheets in XCode 3.2.3, iOS4, with one overlaid on the other.
Also, I get this error in the console (again, only with XCode 3.2.3, iOS4):
wait_fences: failed to receive reply: 10004003
What do you think the issue is?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField tag] >0 )
{
[self dismissKeyboard];
NSString *dateTitle;
dateFieldNumber = [textField tag];
dateTitle = @"Enter your Birth Date";
NSString *title = @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(dateTitle, @"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Ok", nil];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
UIDatePicker *datePicker2 = [[UIDatePicker alloc] init];
self.datePicker = datePicker2;
[datePicker2 release];
self.datePicker.date = [NSDate date];
[self.datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
self.datePicker.datePickerMode = UIDatePickerModeDate;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:60];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-60];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];
[calendar release];
[self.datePicker setMaximumDate:maxDate];
[self.datePicker setMinimumDate:minDate];
[actionSheet addSubview:self.datePicker];
return NO;
} else {
return YES;
}
}
EDIT
As Brian points out below, the textFieldShouldBeginEditing delegate method was being called twice. I can see no reason why this should be the case in IOS4 and not earlier versions. Anyway, I've changed the code so there is a BOOL hasDisplayedSheet, which initially is NO in viewDidLoad. Then I enclosed the the actionsheet creation code (in the the textFieldShouldBeginEditing delegate method) with an if statement, like
if (hasDisplayedSheet == NO) {
// actionsheet code
hasDisplayed = YES
}
and change hasDiplayedSheet
back to NO when they click the sheet button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇帖子提供了添加
的解决方案将 UIDatePicker
转换为可能会直接帮助您的UIActionSheet
。或者这篇帖子看起来更好。
就我个人而言,我永远无法让这些解决方案中的任何一个都能很好地工作,而且我不确定这是否是苹果建议的方法?通过对
UIView
进行动画处理,并将您的UIPickerView
作为子视图,我对这些UIPickerView
视图在 Apple 应用程序中的外观和行为取得了完全的成功和统一,你自己 - 这个博客 帖子帮助我开始了。当我让这种行为起作用时,我添加了一个 OverlayViewController 辅助类来使我的(模态)视图背景半透明;请参阅帖子。请参阅有关 wait_fences 问题的帖子。
This SO post has a solution to adding a
UIDatePicker
to aUIActionSheet
that might help you directly.Or this post, looks better.
Personally, I could never get any of these solutions to work nicely and I'm not sure it would be the approach Apple would suggest? I had complete success and unity with how these
UIPickerView
views look and behave in Apple apps by animating aUIView
, with yourUIPickerView
as a subview, yourself - this blog post helped me get started. When I got this behaviour working I then added an OverlayViewController helper class to make my (modal) view background semi-transparent; see post.See post regarding wait_fences question.