0

アクションシートの日付ピッカーを使用して時間を選択したデータを表示するテーブルビューがあります。

この表では、ラベルとuiswitchボタンが付いたカスタムセル「ReminderCell」を持つ各行を表示します

1.カスタムセル ![ここに画像の説明を入力] [1]

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Note: I set the cell's Identifier property in Interface Builder to DemoTableViewCell.
    ReminderCell *cell = (ReminderCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName];
    if (!cell)
    {
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
    }

    //set from action sheet only this row 0
    if( indexPath.row == 0 ) 
    {
        if(date == nil)
        {
            date = [NSDate date];
        }
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        [timeFormatter setDateFormat:@"hh:mm a"];
        cell.label.text = [timeFormatter stringFromDate:date];
    }

ユーザーが行を選択すると、日付ピッカーがポップアップするアクションシート

![ここに画像の説明を入力してください] [3]

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 

    if(date == nil)
    {
        date = [NSDate date];
    }
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setDateFormat:@"hh:mm a"];

    //Add Action sheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                      destructiveButtonTitle:nil
                                           otherButtonTitles:@"Done",nil];

    // Add date picker to the action sheet
    datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeTime;
    [actionSheet addSubview:datePicker];
    [actionSheet showInView:self.navigationController.tabBarController.view];        
    [actionSheet setBounds:CGRectMake(0,0,320, 550)];
    [datePicker setFrame:CGRectMake(0, 138, 320, 216)];
    [datePicker release];
    [actionSheet release];
}

次に、この日付ピッカーから日付を選択してリマインダーを設定し、スイッチをオンにしたときにオンに設定する必要があります。 前もって感謝します

4

1 に答える 1

1

UIKitは、タスクのより高レベルの抽象化であるNSLocalNotificationオブジェクトを提供します。

    UILocalNotification *yourNotification = [[UILocalNotification alloc] init];
    yourNotification.fireDate = [NSDate date]; // time at which the remainder has to be triggered 
    yourNotification.timeZone = [NSTimeZone defaultTimeZone];

    yourNotification.alertBody = @"Notification triggered";
    yourNotification.alertAction = @"Details";


/* if you wish to pass additional parameters and arguments, you can fill an info dictionary and set it as userInfo property */
    //NSDictionary *infoDict = //fill it with a reference to an istance of NSDictionary;
    //aNotification.userInfo = infoDict;

appDelegate.mで

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    UILocalNotification *aNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (aNotification) {
        //if we're here, than we have a local notification. Add the code to display it to the user
    }

    [self.window makeKeyAndVisible];
    return YES;

}

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

            //if we're here, than we have a local notification. Add the code to display it to the user


    }

アラームを再シャッリングするには

repeatInterval

通知を再スケジュールするカレンダー間隔。

@property(nonatomic)NSCalendarUnitrepeatIntervalディスカッション

週次( NSWeekCalendarUnit)や年次(NSYearCalendarUnit )などのカレンダー単位を割り当てると、システムは指定された間隔で配信の通知を再スケジュールします。デフォルト値は0です。これは、繰り返さないことを意味します。

repeatCalendar

繰り返し通知を再スケジュールするときにシステムが参照する必要のあるカレンダー。

@property(nonatomic、copy)NSCalendar *repeatCalendarディスカッション

デフォルト値はnilで、現在のユーザーカレンダーが使用されていることを示します。(現在のユーザーカレンダーは、NSCalendarのcurrentCalendarクラスメソッドによって返されます。)

とアラームをスケジュールします

    UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
    [onoff addTarget: self action: @selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    // Set the desired frame location of onoff here
    [self.view addSubview: onoff];


- (IBAction)switchAction:(id)sender {

 UISwitch *theSwitch = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
   // From which you can find the index value to get the particular time of the datePicker you set
if (onoff.on){
     NSLog(@"On");  
    //set the alarm at given time from DatePicker using UILocalNotification 
    }
    else{
      NSLog(@"Off");  
    // cancel the alarm Local Notification here
     }
}

参考文献

UILocalNotificationリファレンス

UILocalNotificationチュートリアル

UILocalNotificationプログラミングガイド

UILocalNotificationはLocalNotificationをキャンセルします

于 2012-05-01T10:12:41.123 に答える