Objective c 有什么办法可以去掉这个“;年份“;日期选择者的名字?
我在视图中有一个日期选择器,但我只希望它显示日期和月份,而不是年份,有没有办法将年份去掉?据我所知,UIDatePicker无法做到这一点。您可以使用UIPickerView来实现这一点。要扩展奥斯卡的答案,您确实需要使用UIPickerView 但是,这并不意味着您需要365天编码,您只需要:Objective c 有什么办法可以去掉这个“;年份“;日期选择者的名字?,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我在视图中有一个日期选择器,但我只希望它显示日期和月份,而不是年份,有没有办法将年份去掉?据我所知,UIDatePicker无法做到这一点。您可以使用UIPickerView来实现这一点。要扩展奥斯卡的答案,您确实需要使用UIPickerView 但是,这并不意味着您需要365天编码,您只需要: 12个项目(月)的部件(车轮) 包含31项的组件(天) 选择项目后,您将收到其对代理对象的索引。在这里,您只需要计算出该索引是什么项 就本月而言,这可以通过关键/价值风格机制实现;或者简单地通过其值
- 12个项目(月)的部件(车轮)
- 包含31项的组件(天)
希望这有帮助 正如其他人所指出的:你可以独自创建自己的选择器,但实际上这并不困难。这是一个快速原型,请注意,我使用第一个componet有几天,第二个有几年,这应该根据真实世界应用程序中的语言环境进行:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:pickerView numberOfRowsInComponent:(NSInteger)component
{
NSUInteger number = 0;
if (component == 1) {
number = 12;
} else {
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSUInteger month = [pickerView selectedRowInComponent:1]+1 ;
NSUInteger actualYear = [comps year];
NSDateComponents *selectMothComps = [[NSDateComponents alloc] init];
selectMothComps.year = actualYear;
selectMothComps.month = month;
selectMothComps.day = 1;
NSDateComponents *nextMothComps = [[NSDateComponents alloc] init];
nextMothComps.year = actualYear;
nextMothComps.month = month+1;
nextMothComps.day = 1;
NSDate *thisMonthDate = [[NSCalendar currentCalendar] dateFromComponents:selectMothComps];
NSDate *nextMonthDate = [[NSCalendar currentCalendar] dateFromComponents:nextMothComps];
NSDateComponents *differnce = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:thisMonthDate
toDate:nextMonthDate
options:0];
number = [differnce day];
}
return number;
}
-(void)pickerView:pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 1) {
[pickerView reloadComponent:0];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:@"%d", row+1];
}
您可以在以下位置找到示例代码:
您可以在GitHub上找到的版本知道当月最受欢迎语言的名称
我在GitHub上找到了这个项目:Swift 3.0版本:
@IBOutlet weak var pickerCell: UIPickerView!
var months: Array<String> = Calendar.current.monthSymbols
func config() {
pickerCell.delegate = self
pickerCell.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var number = 0
if component == 1 {
return 12
} else if pickerView.numberOfComponents > 1 {
let comps = Calendar.current.dateComponents([.day,.month, .year], from: Date())
let month: Int = pickerView.selectedRow(inComponent: 1)+1
let year: Int = comps.year!
var selectMothComps = DateComponents()
selectMothComps.year = year
selectMothComps.month = month
selectMothComps.day = 1
var nextMothComps = DateComponents()
nextMothComps.year = year
nextMothComps.month = month+1
nextMothComps.day = 1
let thisMonthDate = Calendar.current.date(from: selectMothComps)
let nextMonthDate = Calendar.current.date(from: nextMothComps)
var difference = Calendar.current.dateComponents([.day], from: thisMonthDate!, to: nextMonthDate!)
number = difference.day!
}
return number
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 1 {
pickerView.reloadComponent(0)
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0{
return String(format: "%d", row+1)
} else {
return months[row]
}
}
//centers the views. width > than width of smallest component
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
if component == 0 {
return 45
}
return 140
}
@IBOutlet弱var pickerCell:UIPickerView!
变量月份:数组=Calendar.current.monthSymbols
func config(){
pickerCell.delegate=self
pickerCell.dataSource=self
}
func numberOfComponents(在pickerView:UIPickerView中)->Int{
返回2
}
func pickerView(pickerView:UIPickerView,numberOfRowsInComponent:Int)->Int{
变量编号=0
如果组件==1{
返回12
}否则,如果pickerView.numberOfComponents>1{
让comps=Calendar.current.dateComponents([.day、.month、.year],from:Date())
月份:Int=pickerView.selectedRow(不完整:1)+1
让年份:Int=comps.year!
var selectMothComps=DateComponents()
选择MOTH COMPS.year=年份
选择MOTH COMPS.MOUNT=MOUNT
选择MothComps.day=1
var nextMothComps=DateComponents()
下一个公司年度=年度
下一个月=月份+1
下一个交易日=1
让thisMonthDate=Calendar.current.date(从:选择MothComps)
让nextMonthDate=Calendar.current.date(from:NextMonthComps)
var difference=Calendar.current.dateComponents([.day],from:thisMonthDate!,to:nextMonthDate!)
数字=差。天!
}
返回号码
}
func pickerView(pickerView:UIPickerView,didSelectRow行:Int,不完整组件:Int){
如果组件==1{
pickerView.reloadComponent(0)
}
}
func pickerView(pickerView:UIPickerView,titleForRow行:Int,forComponent组件:Int)->String?{
如果组件==0{
返回字符串(格式:“%d”,第+1行)
}否则{
返回月份[行]
}
}
//使视图居中。宽度>小于最小组件的宽度
func pickerView(pickerView:UIPickerView,widthForComponent:Int)->CGFloat{
如果组件==0{
返回45
}
返回140
}
你能给我们看一下代码吗,这样我们就可以通过看你是怎么做到的来帮助我们了?@adrianphillips我还没有代码,我在故事板的视图中只有日期选择器对象……看看我的答案Gabriel,它有几个链接可以帮助你。那么,日期选择器用来显示年份?现在这似乎都不是一个选项!«两个或更多,使用for»-Edsger W.Dijkstra我想说的是我无法测试代码,但不管怎样,我认为你的答案是好的。我恭维你,并投票支持你的答案,作为回报,你以一种消极的、咄咄逼人的方式回答。很好,为什么这么咄咄逼人?你说,你不担保,我说,我担保?