以下代碼在XCode 3.2.2,iPhone OS 3.1.3中創建一個UIActionSheet。嵌入到操作表中的是日期選擇器。iOS4創建兩個UIActionSheets,3.1.3創建一個?爲什麼?
但似乎在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;
}
}
EDIT
正如下面布賴恩指出,textFieldShouldBeginEditing委託方法正在調用兩次。我可以看到沒有理由說明爲什麼IOS4會出現這種情況,而不是早期版本。無論如何,我已經更改了代碼,所以有一個BOOL hasDisplayedSheet,最初在viewDidLoad中是NO。然後我用if語句所包圍的所述actionsheet創建代碼(在該textFieldShouldBeginEditing委託方法),如
if (hasDisplayedSheet == NO) {
// actionsheet code
hasDisplayed = YES
}
和更改hasDiplayedSheet
回NO時,他們點擊片按鈕。
只是試圖排除顯而易見的 - 是'textFieldShouldBeginEditing:'被調用兩次嗎? – Brian 2010-06-30 13:15:15
你說得對。它會調用textFieldShouldBeginEditing兩次。我不知道爲什麼它會在IOS4中做到這一點,而不是在以前的操作系統中。 – cannyboy 2010-06-30 19:44:38