本地通知未调用

时间:2015-01-07 10:05:02

标签: ios iphone

我想实现localNotification.It不重新加载我的表。请提前帮助谢谢。我添加项目然后保存项目。它没有重载表它变成了空白。我的调试器没有达到重载数据功能。

在我的ViewController1.m

-(IBAction)save:(id)sender
{
    NSDate *date=[datepicker date];
    NSString *taskString =task.text;
    UILocalNotification *localnotification=[[UILocalNotification alloc]init];
    localnotification.fireDate=date;
    localnotification.alertBody=taskString;
    localnotification.alertAction=@"Show Me The Item";
    localnotification.timeZone=[NSTimeZone defaultTimeZone];
    localnotification.applicationIconBadgeNumber=[[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
    [[UIApplication sharedApplication]scheduleLocalNotification:localnotification];



   [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];


    [self dismissViewControllerAnimated:YES completion:nil];

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadTable)
                                                 name:@"reloadData"
                                               object:nil];

    // 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)add:(id)sender
{
    ViewController1 *sec=[[ViewController1 alloc]initWithNibName:@"ViewController1" bundle:nil];
    [self presentViewController:sec animated:YES completion:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[UIApplication sharedApplication ]scheduledLocalNotifications]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"ViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Get list of local notifications
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    UILocalNotification *localNotification = [localNotifications objectAtIndex:indexPath.row];

    // Display notification info
    [cell.textLabel setText:localNotification.alertBody];
    [cell.detailTextLabel setText:[localNotification.fireDate description]];

    return cell;


}
-(void)reloadTable
{
    [table reloadData];
}

1 个答案:

答案 0 :(得分:0)

您应该使用NSNotificationCenter。它就像一个无线电天线。首先,您应该在viewDidLoad注册到此电台:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadData" object:nil];

然后在您的不同ViewController上发送此无线电信号,该信号将被所有类接收,并监听此电台。发送这样的信息:

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];

现在,您的ViewController将按需重新加载数据。