0

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    
4

2 に答える 2

0

データ構造、できればキーが日付で、値がアラートに表示する文字列である辞書が必要です。日付ピッカーで日付が選択されたら、その日付に一致するキーをディクショナリで検索し、そのキーの値をアラート ビューのメッセージに割り当てます。

于 2013-07-26T04:34:50.573 に答える
0

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」の可能性があります。

于 2013-07-25T23:50:03.257 に答える