【问题标题】:Custom alertview based on date selected in datepicker基于日期选择器中选择的日期的自定义警报视图
【发布时间】:2013-07-25 23:18:33
【问题描述】:

刚开始学习Obj-C;如果有任何幼稚的问题,请原谅。

我正在尝试创建一个应用程序,该应用程序根据在日期选择器中选择的日期显示自定义警报视图。

这是我现在已经有权的代码,它在选择任何日期时显示一个硬编码的警报浏览量,并删除按钮。我怎样才能使依赖于所选日期。

(#)import "APViewController.h"
@interface APViewController ()
@end

@implementation APViewController
@synthesize datePicker;

- (void)viewDidLoad
{
[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)specialButton:(id)sender {

//  NSDate *chosen = [datePicker date];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:@"Its your   50th Birthday" delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil];

[alert show];

}

@end    

【问题讨论】:

  • “取决于所选日期”是什么意思?您希望该日期出现在警报中吗?
  • @rdelmar 根据在选择器中选择的日期,alertview 必须发出警报。如果选择了 7 月 2 日,则应该说 8 月 10 日(无论何时)生日快乐妈妈和爸爸生日快乐。警报在按下按钮时执行。

标签: ios objective-c datepicker


【解决方案1】:

使用 NSDateFormatter 将日期选择器的日期格式化为您想要的格式,然后在警报中显示:

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"cccc, MMM d, hh:mm aa"];
NSString * dateString = [dateFormatter stringFromDate:datePicker.date];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:dateString delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil];

[alert show];

如果您试图显示用户的年龄,假设他们将日期选择器放在他们的生日上,请使用 NSDateComponents 来获取日期的年份以及当前年份。

NSDateComponents * currentDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
NSDateComponents * pickedDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:datePicker.date];

NSInteger diff = currentDateComponents.year - pickedDateComponents.year;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:[NSString stringWithFormat:@"Its your %dth Birthday", diff] delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil];

[alert show];

您必须确保它并不总是“th”。它可以是“st”或“nd”或“rd”。

【讨论】:

    【解决方案2】:

    您需要一个数据结构,最好是一个字典,其中键是日期,值是要在警报中显示的字符串。在日期选择器中选择日期后,在字典中搜索与该日期匹配的键,并将该键的值分配给警报视图中的消息。

    【讨论】:

    • 感谢您的帮助。您能否提供一些示例代码。我该怎么做“在日期选择器中选择日期时,在字典中搜索与该日期匹配的键”
    • @aceprogramer,如果您先尝试自己解决问题,您会学得更好,如果您无法解决问题,请返回具体问题。您可以使用 NSDictionary 方法 allKeys 来获取一个键数组,您可以循环遍历该数组,并测试与所选日期的等效性。试试看。
    • 我一直在处理相同的问题,但无法为每个选定的日期附加值。我这样做了,但对如何将密钥连接到所选日期感到困惑。 `- (void)viewDidLoad { [super viewDidLoad]; events = [[NSArray alloc] initWithObjects:@"Awesome", @"Great",@"Decent", @"Surviving", @"Bad", nil];日期 = [[NSDictionary alloc] init]; date_keys = [日期 allKeys]; }
    • 这种方式也试过了,当然不行。 - (IBAction)specialButton:(id)sender { NSDate *chosen = [datePicker date] NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [格式化程序 setDateFormat:@"MMMd"]; NSString *weekday = [formatter stringFromDate:chosen]; NSString *bday = [[NSString alloc]initWithFormat:@"Jul26"]; if (weekday == bday) { UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"今天是几号?"message:@"爸爸的生日"delegate:nilcancelButtonTitle:@"Thanks"otherButtonTitles: nil]; [我的警报节目]; } }`
    • @aceprogramer,你不应该对格式化程序做任何事情,你想坚持使用 NSDate 对象作为你的键。但是你在哪里创建你的字典?在上面显示的代码中,您分配了一个字典,然后在下一行调用 allKeys ——此时字典为空,没有键。您需要使用 setObject:forKey: (每对一次)或 dictionaryWithObjects:forKeys: 构造字典,您可以在其中传递对象数组和键数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2020-04-11
    相关资源
    最近更新 更多