Iphone 使用UIswitch在操作表的表视图中设置提醒

Iphone 使用UIswitch在操作表的表视图中设置提醒,iphone,objective-c,push-notification,uiactionsheet,uilocalnotification,Iphone,Objective C,Push Notification,Uiactionsheet,Uilocalnotification,我有一个表视图,它显示我们在操作表中使用日期选择器选择时间的数据 在此表视图中,每一行都有一个自定义单元格“提醒单元格”,带有标签和uiswitch按钮 1.自定义单元格 ![在此处输入图像描述][1] - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Note: I set the cell's Identifier pr

我有一个表视图,它显示我们在操作表中使用日期选择器选择时间的数据

在此表视图中,每一行都有一个自定义单元格“提醒单元格”,带有标签和uiswitch按钮

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];
}
现在我需要通过从这个日期选择器中选择日期来设置提醒,并在打开时设置提醒。
提前感谢

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


    }
重新关闭警报

重复间隔

重新安排通知的日历间隔

@属性(非原子)重复间隔 讨论

如果您指定了日历单位,如每周()或每年(),系统将重新安排通知在指定的时间间隔发送。默认值为0,表示不重复

重复日历

系统重新安排重复通知时应参考的日历

@属性(非原子,副本)*重复日历 讨论

默认值为nil,表示使用了当前用户日历。(当前用户日历由NSCalendar的类方法返回。)

并安排闹钟

    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
     }
}
参考资料