#iphone #objective-c #ios #cocoa-touch #ios4
#iPhone #objective-c #iOS #cocoa-touch #ios4
Вопрос:
ОТРЕДАКТИРОВАНО :-
Извините за повторное редактирование вопроса, но на самом деле я не получаю ответа, что мне нужно. Я думаю, что мой способ задать вопрос был неправильным, поэтому переписываю весь вопрос :
Я создал приложение, в котором настроено приложение для сигнализации. UILocalNotification должен вызываться во время, выбранное мной в DatePicker, но не вызывается в правильное время. Например, я выбрал время как 2: 00 вечера для будильника, поэтому уведомление будет вызываться между 2: 00 вечера и 2: 01 вечера … но не уверен, когда… это дает мне задержку случайного времени. В моем TableView вы можете видеть, что отображаемое описание также неверно. Я знаю, что я из Индии, поэтому он показывает время по Гринвичу, но можно ли это исправить?
Вот весь мой код:-
——————————AppDelegate.m файл:-——————————
@synthesize window,viewController,timeViewController;
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"Mayank"];
if( x == 1 )
{
timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect frame = timeViewController.view.frame;
frame.origin = CGPointMake(frame.origin.x,frame.origin.y statusBarFrame.size.height );
timeViewController.view.frame = frame;
[self.window addSubview:timeViewController.view];
}
else
{
[[NSUserDefaults standardUserDefaults]setInteger:1 forKey:@"Mayank"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to set the Default Alarm?" message:@"at 4:20 PM" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[alert show];
[alert release];
}
sleep(1);
[self.window makeKeyAndVisible];
application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
NSString *reminderText = [localNotification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
timeViewController = [[TimeViewController alloc]initWithNibName:@"TimeViewController" bundle:nil];
timeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect frame = timeViewController.view.frame;
frame.origin = CGPointMake(frame.origin.x,frame.origin.y statusBarFrame.size.height );
timeViewController.view.frame = frame;
[self.window addSubview:timeViewController.view];
}
if(buttonIndex == 1)
{
viewController = [[SetAlarmViewController alloc]initWithNibName:@"SetAlarmViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect frame = viewController.view.frame;
frame.origin = CGPointMake(frame.origin.x,frame.origin.y statusBarFrame.size.height );
viewController.view.frame = frame;
[self.window addSubview:viewController.view];
}
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
application.applicationIconBadgeNumber = 0;
}
——————————Файл MainViewController.m:-——————————
@implementation SetAlarmViewController
@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (The420DudeAppDelegate *)[[UIApplication sharedApplication] delegate];
eventText.returnKeyType = UIReturnKeyDone;
// datePicker.minimumDate = [NSDate date];
NSDate *now = [NSDate date];
[datePicker setDate:now animated:YES];
eventText.delegate = self;
index = 0;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self.tableview reloadData];
}
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = pickerDate;
// NSLog(@"%@",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// NSLog(@"%@",localNotif.timeZone);
// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = @"Show me";
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.soundName = @"jet.wav";
// Specify custom data for the notification
NSDictionary *userDict = [NSDictionary dictionaryWithObject:eventText.text
forKey:kRemindMeNotificationDataKey];
localNotif.userInfo = userDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[self.tableview reloadData];
eventText.text = @"";
viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil];
[self presentModalViewController:viewController animated:YES];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
index = indexPath.row;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning!!!"
message:@"Are you sure you want to Delete???" delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok",nil];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notify = [notificationArray objectAtIndex:index];
if(buttonIndex == 0)
{
// Do Nothing on Tapping Cancel...
}
if(buttonIndex ==1)
{
if(notify)
[[UIApplication sharedApplication] cancelLocalNotification:notify];
}
[self.tableview reloadData];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
[cell.textLabel setText:notif.alertBody];
[cell.detailTextLabel setText:[notif.fireDate description]];
return cell;
}
- (void)showReminder:(NSString *)text {
/*
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder"
message:@"hello" delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[self.tableview reloadData];
[alertView release];
*/
NSString *path = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
[player play];
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet showInView:self.view];
// [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[player stop];
if(buttonIndex == 0)
{
NSLog(@"OK Tapped");
}
if(buttonIndex == 1)
{
NSLog(@"Cancel Tapped");
}
}
На этом рисунке показан вид моего приложения :-
Этот метод не вызывается
Ответ №1:
Я думаю, что проблема здесь связана с тем, что NSDatePicker возвращает секунды текущего времени вместе с выбранным временем. Вам нужно отсчитать секунды от даты, возвращаемой NSDatePicker, и использовать это для вашего будильника и локального уведомления.
NSDate *selectedDate = [datePicker date]; // you don't need to alloc-init the variable first
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *dc = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate];
selectedDate = [cal dateFromComponents:dc]; // now you have an NSDate with zero seconds for your alarm
Вы должны получить гораздо более точную информацию о своих уведомлениях, но я не думаю, что они гарантированно будут выполняться с точностью до доли секунды.
Ответ №2:
Это проблема:
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [datePicker date];
Вы пропускаете NSDate, который вы выделяете, а затем выбрасываете. Избавьтесь от alloc / init и просто назначьте nil
указателю. Но это не объясняет вашу большую проблему.
Помимо этого, либо вы что-то не показываете нам, либо средство выбора даты все еще перемещается при его выборке.
Комментарии:
1. хехее … я тоже прочитал твой предыдущий комментарий. 🙂 черт возьми, чувак, на самом деле я написал много кода, так что, возможно, я забыл исправить и это.
2. Что еще вам нужно …. должен ли я вставить весь свой код … это всего лишь небольшой проект, только один контроллер просмотра …. я вставил только основную часть, так как она не должна видеть длинный вопрос, иначе какой-нибудь гениальный отклонил его .. 🙁
3. Я подозреваю, что вы не опубликовали весь метод scheduleAlarm. Или же отправленные вами сообщения не соответствуют этой версии кода. (Извините, что кажусь таким подозрительным, но мы постоянно видим подобные вещи.)
4. Вы в Индии, верно? Это объясняет разницу между первыми двумя значениями времени. Единственное необъяснимое значение — это значение 10: 10:26, и это на самом деле даже не обязательно для кода, поскольку вы получили дату из DatePicker ранее. Я подозреваю какую-то ситуацию с неинициализированной переменной, но я ее не вижу.
Ответ №3:
Если вы просто вызовете description
объект NSDate (что вы и делаете в своем методе cellForRowAtIndexPath), вы получите внутреннее представление даты, которое находится в GMT. Вам нужно использовать NSDateFormatter
его язык для устройства язык и используйте это stringFromDate:
способ получить нужные для отображения.
Комментарии:
1. можете ли вы дать мне какой-нибудь код или ссылку или какой-нибудь намек … как и в моем коде, я перезагружаю таблицу, чтобы она устанавливала все строки с этим измененным временем. 🙁