gpt4 book ai didi

objective-c - iPhone 6s plus 设备中的 UIPickerView UI 和功能问题

转载 作者:太空狗 更新时间:2023-10-30 03:33:36 25 4
gpt4 key购买 nike

我使用 UIPickerView 创建了自定义 TimePicker,使用了 3 个 UIPickerView

  • 一个小时
  • 一分钟
  • 一个用于上午/下午

通过选择时间,我正在使用该时间来设置本地通知。因为我的应用程序是基于警报功能的。

UI 部分和功能部分在 iPhone 5、5s、6、6 Plus 中运行良好,但在 iPhone 6s plus 设备中面临问题。不知道出了什么问题!相同的代码适用于除 iPhone 6s plus 以外的所有其他设备。

下面是 iPhone 6 的截图,

enter image description here

如您所见,ScreenShot 时间和 PickerView 时间相同。它是功能部件。我已经编写了代码,如捕获当前时间并根据我设置了 PickerView 的选定索引。工作完美!我也将选定的索引字体设置得更大。这在 iPhone 6 设备中看起来也很完美。

下图为 iPhone 6s Plus 截图,

enter image description here

如您所见,ScreenShot 时间和 PickerView 时间不相同。所以功能部分在 iPhone 6s plus 设备上不起作用。同样在 UI 部分,字体在第二个 PickerView 中的选定行变大,但在第一个中没有。

此外,当我通过从时间选择器中选择时间来设置本地通知时。它也适用于 iPhone 6s plus 以外的所有其他设备(通知在完美时间触发)。在 iPhone 6s 以及我从 UIPickerView 中选择的任何时间,通知都会立即触发。

不知道 iPhone 6s plus 设备出了什么问题。这两个问题都与 PickerView 或其他问题有关。但为什么相同的代码适用于所有其他设备,而不仅仅是适用于 iPhone 6s plus 设备。

感谢任何帮助!严重卡在这个问题上。如果有人对此问题有任何解决方案,请提供帮助!提前致谢。

更新:

请查看我的代码...

AddAlarmViewController.h

@interface AddAlarmViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
{
}

@property (strong,nonatomic) NSArray *arrHours;
@property (strong,nonatomic) NSArray *arrMinute;
@property (strong,nonatomic) NSArray *arrAP;


@property (nonatomic,strong) IBOutlet UIPickerView *myPickerHour;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerMinute;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerAP;

AddAlarmViewController.m

@interface AddAlarmViewController ()
{
int countH,countM,countAP;
}

@property (strong,nonatomic) NSString *strTime,*strTimeSlot;
@property (strong,nonatomic) NSDate *dateSelected;
@property (assign,nonatomic) NSInteger rowHour,rowAP,rowMinute;
@property (strong,nonatomic) UILabel *lblHour,*lblMinute,*lblAP;
@end

@implementation AddAlarmViewController

- (void)viewDidLoad {

[super viewDidLoad];

countH = 0;
countM = 0;
countAP = 0;

_arrHours = @[[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10],[NSNumber numberWithInt:11],[NSNumber numberWithInt:12]];

_arrMinute = @[[NSNumber numberWithInt:0],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10],[NSNumber numberWithInt:11],[NSNumber numberWithInt:12],[NSNumber numberWithInt:13],[NSNumber numberWithInt:14],[NSNumber numberWithInt:15],[NSNumber numberWithInt:16],[NSNumber numberWithInt:17],[NSNumber numberWithInt:18],[NSNumber numberWithInt:19],[NSNumber numberWithInt:20],[NSNumber numberWithInt:21],[NSNumber numberWithInt:22],[NSNumber numberWithInt:23],[NSNumber numberWithInt:24],[NSNumber numberWithInt:25],[NSNumber numberWithInt:26],[NSNumber numberWithInt:27],[NSNumber numberWithInt:28],[NSNumber numberWithInt:29],[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:32],[NSNumber numberWithInt:33],[NSNumber numberWithInt:34],[NSNumber numberWithInt:35],[NSNumber numberWithInt:36],[NSNumber numberWithInt:37],[NSNumber numberWithInt:38],[NSNumber numberWithInt:39],[NSNumber numberWithInt:40],[NSNumber numberWithInt:41],[NSNumber numberWithInt:42],[NSNumber numberWithInt:43],[NSNumber numberWithInt:44],[NSNumber numberWithInt:45],[NSNumber numberWithInt:46],[NSNumber numberWithInt:47],[NSNumber numberWithInt:48],[NSNumber numberWithInt:49],[NSNumber numberWithInt:50],[NSNumber numberWithInt:51],[NSNumber numberWithInt:52],[NSNumber numberWithInt:53],[NSNumber numberWithInt:54],[NSNumber numberWithInt:55],[NSNumber numberWithInt:56],[NSNumber numberWithInt:57],[NSNumber numberWithInt:58],[NSNumber numberWithInt:59]];

_arrAP = @[@"AM",@"PM"];

[self.myPickerHour reloadAllComponents];
[self.myPickerHour selectRow:_rowHour inComponent:0 animated:YES];

[self.myPickerMinute reloadAllComponents];
[self.myPickerMinute selectRow:_rowMinute inComponent:0 animated:YES];

[self.myPickerAP reloadAllComponents];
[self.myPickerAP selectRow:_rowAP inComponent:0 animated:YES];
}


#pragma mark - Picker View Data source

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{

if (pickerView.tag == 1) {
return [_arrHours count] * 100;
} else if (pickerView.tag == 2) {
return [_arrMinute count] * 100;
} else {
return [_arrAP count];
}
}

#pragma mark- Picker View Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component {

if (pickerView.tag == 1) {
NSLog(@" Row Number %ld", row % [_arrHours count]);
_lblHour = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblHour setFont:[UIFont fontWithName:@"OpenSans-Light" size:70.0]];
[_lblHour setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of hour : %@",_lblHour.text);
if (countH == 4) {
isHour = TRUE;

//------------------Above and Below Rows------------------//
if (row > 0) {
UILabel *lbl = (UILabel *)[pickerView viewForRow:row-1 forComponent:component];
[lbl setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor darkGrayColor];
}
if (row < (_arrHours.count * 100)) {
UILabel *lbl1 = (UILabel *)[pickerView viewForRow:row+1 forComponent:component];
[lbl1 setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.textColor = [UIColor darkGrayColor];
}
//-------------------------------------------------------//
}

} else if (pickerView.tag == 2) {
_lblMinute = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblMinute setFont:[UIFont fontWithName:@"OpenSans-Light" size:70.0]];
[_lblMinute setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of minute : %@",_lblMinute.text);
if (countM == 4) {
isMinute = TRUE;

//------------------Above and Below Rows------------------//
if (row > 0) {
UILabel *lbl = (UILabel *)[pickerView viewForRow:row-1 forComponent:component];
[lbl setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor darkGrayColor];
}

if (row < (_arrMinute.count * 100)) {
UILabel *lbl1 = (UILabel *)[pickerView viewForRow:row+1 forComponent:component];
[lbl1 setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.textColor = [UIColor darkGrayColor];
}
//-------------------------------------------------------//

}

} else if (pickerView.tag == 3) {

_lblAP = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblAP setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of AP : %@",_lblAP.text);
}
NSLog(@"----------4");
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd/MM/yyyy"];
NSString *str = [format stringFromDate:[NSDate date]];

NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];

[format setDateFormat:@"hh:mm a"];
NSString *strHM = [format stringFromDate:now];

NSString *str1 = [NSString stringWithFormat:@"%@:%@ %@",_lblHour.text,_lblMinute.text,_lblAP.text];
NSString *strSelected = [NSString stringWithFormat:@"%@:%@ %@",_lblHour.text,_lblMinute.text,_lblAP.text];
if ([strHM isEqualToString:strSelected]) {
[format setDateFormat:@"dd/MM/yyyy"];
NSString *strWithOneDay = [format stringFromDate:newDate1];
_strTime = [NSString stringWithFormat:@"%@ %@",strWithOneDay,str1];
} else {
_strTime = [NSString stringWithFormat:@"%@ %@",str,str1];
}

[format setDateFormat:@"dd/MM/yyyy hh:mm a"];
_dateSelected = [format dateFromString:_strTime];

if (!(_dateSelected == NULL)) {

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:_dateSelected];
NSInteger hour = [components hour];

if(hour >= 3 && hour < 12) {
_strTimeSlot = @"GOOD MORNING";
}
else if(hour >= 12 && hour < 17) {
_strTimeSlot = @"GOOD AFTERNOON";
}
else if(hour >= 17 && hour < 20) {
_strTimeSlot = @"GOOD EVENING";
} else {
_strTimeSlot = @"GOOD NIGHT";
}
}
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if (pickerView.tag == 1 || pickerView.tag == 2) {
return 70.0;
}
return 30.0;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = (id)view;

if (pickerView.tag == 1) {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];

lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor darkGrayColor];
if ([[NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]] length] == 1) {
NSString *str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]]];
lblTitle.text = str;
} else {
lblTitle.text = [NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]];
}
if (row == _rowHour && !isHour) {
countH++;
[self pickerView:self.myPickerHour didSelectRow:_rowHour inComponent:0];
_lblHour.text = lblTitle.text;
}

} else if (pickerView.tag == 2) {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];

lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor darkGrayColor];
if ([[NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]] length] == 1) {
NSString *str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]]];
lblTitle.text = str;
} else {
lblTitle.text = [NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]];
}
if (row == _rowMinute && !isMinute) {
countM++;
[self pickerView:self.myPickerMinute didSelectRow:_rowMinute inComponent:0];
_lblMinute.text = lblTitle.text;
}

} else {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];

lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor blackColor];
lblTitle.text = [_arrAP objectAtIndex:row];
if (row == _rowAP) {
[self pickerView:self.myPickerAP didSelectRow:_rowAP inComponent:0];
_lblAP.text = lblTitle.text;
}
}
return lblTitle;
}

最佳答案

这个答案是为 future 的读者准备的,可能会对某些人有所帮助。

经过大量的研发和调试代码,终于解决了我的问题。

我在当前(检查上述问题的代码)代码中添加的代码如下,

NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setLocale:locale];
[format setTimeZone:[NSTimeZone systemTimeZone]];

在添加以上代码行之前(在我使用过 NSDateFormatter 的地方添加了这些行)它在除 iPhone 6s plus 之外的所有其他设备上都运行良好。但是,通过将 NSLocaleTimeZone 设置为 NSDateFormatter,它在所有 设备上都能正常工作。

谢谢!

关于objective-c - iPhone 6s plus 设备中的 UIPickerView UI 和功能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37672317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com