2012-01-05 37 views
5

其实我正在制作一个闹钟应用程序。在那当我设置时间UILocalNotification事件发生在那个时候,它调用AppDelegate类即didReceiveNotifications方法的方法。在这个方法中,我写了一个代码来调用SetViewController(showReminder方法)的一个方法,现在在这个方法中,我希望它应该显示一个NewViewController,即TimeViewController,因为我必须在警报调用时显示动画。要显示一个FirstViewController在调用iPhone中的SecondViewController的方法

我需要这个当Alarm被调用时,我已经设置了一个动作表来显示,但我想显示动画also.Action表单出现在所有视图但动画可以只显示在特定的视图,这就是为什么我需要显示一个不同的ViewController。

这里是我想做的代码: - ?我已经尝试了这些人也喜欢PresentModalViewController,dismissModalViewController,AddSubview ,删除的SuperView ......但结果都是阴性:(我应该怎么办..

几乎整个代码: -

AppDelegate类: -

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    if (notification){ 
     NSLog(@"In did Notification"); 
     NSString *reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey]; 
     [viewController showReminder:reminderText]; 
     application.applicationIconBadgeNumber = 0; 
    } 
} 

setViewController.h: -

@interface SetAlarmViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIActionSheetDelegate>{ 

    IBOutlet UITableView *tableview; 
    IBOutlet UIDatePicker *datePicker; 
    IBOutlet UITextField *eventText; 
    TPKeyboardAvoidingScrollView *scrollView; 

    IBOutlet UINavigationBar *titleBar; 
    IBOutlet UIButton *setAlarmButton; 
    AVAudioPlayer *player; 
    int index; 
    The420DudeAppDelegate *appDelegate; 
    TimeViewController *viewController; 

    IBOutlet UIImageView *animatedImages; 

    NSMutableArray *imageArray; 
    AVPlayerItem *player1,*player3; 
    AVPlayerItem *player2,*player4; 
    AVQueuePlayer *queuePlayer; 
} 
@property (nonatomic, retain) IBOutlet UIImageView *animatedImages; 

@property (nonatomic, retain) IBOutlet UITableView *tableview; 
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker; 
@property (nonatomic, retain) IBOutlet UITextField *eventText; 
@property (nonatomic, retain) TPKeyboardAvoidingScrollView *scrollView; 

@property(nonatomic, retain) IBOutlet UINavigationBar *titleBar; 
@property(nonatomic, retain) IBOutlet UIButton *setAlarmButton; 
@property(nonatomic) UIReturnKeyType returnKeyType; 

@property(nonatomic, retain) IBOutlet TimeViewController *viewController; 

- (IBAction) scheduleAlarm:(id)sender; 
- (void)showReminder:(NSString *)text; 
-(IBAction)onTapHome; 

-(IBAction)onTapChange:(id)sender; 

@end 

SetViewController.m: -

@synthesize datePicker,tableview, eventText,titleBar,setAlarmButton,returnKeyType,scrollView,animatedImages,viewController; 


// 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; 

    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil]; 

    NSDate *now = [NSDate date]; 
    [datePicker setDate:now animated:YES]; 
    eventText.delegate = self; 
    index = 0; 

    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"inhale" ofType:@"mp3"]; 
    NSURL *url1 = [NSURL fileURLWithPath:path1]; 
    player1 = [[AVPlayerItem alloc]initWithURL:url1]; 

    NSString *path3 = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"]; 
    NSURL *url3 = [NSURL fileURLWithPath:path3]; 
    player3 = [[AVPlayerItem alloc]initWithURL:url3]; 

    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"exhale" ofType:@"mp3"]; 
    NSURL *url2 = [NSURL fileURLWithPath:path2]; 
    player2 = [[AVPlayerItem alloc]initWithURL:url2]; 

    NSString *path4 = [[NSBundle mainBundle] pathForResource:@"Dude" ofType:@"mp3"]; 
    NSURL *url4 = [NSURL fileURLWithPath:path4]; 
    player4 = [[AVPlayerItem alloc]initWithURL:url4]; 


    NSArray *items = [[NSArray alloc]initWithObjects:player1,player3,player2,player4,nil]; 
    queuePlayer = [[AVQueuePlayer alloc] initWithItems:items];  

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEnded) name:AVPlayerItemDidPlayToEndTimeNotification object:player4]; 



} 

-(void)onAlarmInvoke 
{ 
    animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    animatedImages.userInteractionEnabled = YES; 
    [animatedImages setContentMode:UIViewContentModeScaleToFill]; 
    [self.view addSubview : animatedImages]; 

    [queuePlayer play];  

    // Array to hold jpg images 
    imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT]; 

    // Build array of images, cycling through image names 
    for (int i = 1; i <= IMAGE_COUNT; i++) 
     [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"animation(%d).jpg", i]]]; 

    animatedImages.animationImages = [NSArray arrayWithArray:imageArray]; 

    // One cycle through all the images takes 1.0 seconds 
    animatedImages.animationDuration = 12.0; 

    // Repeat foreverlight electro/4 sec. 
    animatedImages.animationRepeatCount = -1; 

    // Add subview and make window visible 
    // [self.view addSubview:animatedImages]; 

    animatedImages.image = [imageArray objectAtIndex:imageArray.count - 1]; 

    // Start it up 
    [animatedImages startAnimating]; 



    // Wait 5 seconds, then stop animation 
    [self performSelector:@selector(stopAnimation) withObject:nil afterDelay:15000]; 

} 

-(void)playEnded 
{ 
    [self performSelector:@selector(playNextItem) withObject:nil afterDelay:5.0]; 
} 

-(void)playNextItem 
{ 
    [queuePlayer play]; 
} 
-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [scrollView adjustOffsetToIdealIfNeeded]; 
} 

-(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]; 

    // 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:pickerDate]; 
    pickerDate = [cal dateFromComponents:dc]; 

    NSLog(@"%@ is the date in picker date",pickerDate); 

    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]; 
} 


- (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)viewDidUnload { 
    datePicker = nil; 
    tableview = nil; 
    eventText = nil; 
    [self setScrollView:nil]; 
    [super viewDidUnload]; 

} 

- (void)showReminder:(NSString *)text { 

    [self onAlarmInvoke]; 

    [self.view addSubview:viewController.view]; 

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    CGRect rect = self.view.frame; 
    // if(rect.origin.y <= 480) 
    //  rect.origin.y +=20; 

    self.view.frame = rect; 
    [actionSheet showInView:self.view]; 
    [actionSheet release]; 


} 
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    if(buttonIndex == 0) 
    { 
     [player stop]; 
     NSLog(@"OK Tapped"); 
    } 
    if(buttonIndex == 1) 
    { 
     [player stop]; 
     NSLog(@"Cancel Tapped"); 
    } 

} 

-(IBAction)onTapHome{ 
    viewController = [[TimeViewController alloc] initWithNibName:@"TimeViewController" bundle:nil]; 
    [self presentModalViewController:viewController animated:YES]; 
} 

- (void)dealloc { 
    [super dealloc]; 
    [datePicker release]; 
    [tableview release]; 
    [eventText release]; 
    [scrollView release]; 
} 
-(IBAction)onTapChange:(id)sender{ 

    SetTimeViewController *viewC = [[SetTimeViewController alloc]initWithNibName:@"SetTimeViewController" bundle:nil]; 
    [self presentModalViewController:viewC animated:YES]; 
} 
@end 
+0

对不起,如果我没有得到你,但[self.view removeFromSubview];应该解决 – 2012-01-05 07:38:49

+0

没有家伙...其不..我尝试过所有的品种:P像PresentModalViewController,dismissModalViewController,AddSubview,删除superView ...但负面的结果。 :( – mAc 2012-01-05 07:46:22

+0

在你的appDelegate中,你的viewController是否已经分配并初始化? – Canopus 2012-01-06 14:27:24

回答

1

你的viewController可能会显示该视图,但如果没有SetViewController的视图,屏幕上就看不到它。你将不得不先去SetViewController,然后展示你的TimeViewController。这是对的,你想显示SetViewController,但立即调用showReminder:方法?但仅限于didReceiveLocalNotification :.

如果是这种情况,设置一个标志,并在SetViewControllers text属性.H,

BOOL isFromNotification; 
NSString *notifText; 

并呈现SetViewController,并设置标志

SetViewController *setViewController = [SetViewController alloc]........ 
setViewController.isFromNotification = YES; 
setViewController.notifText = reminderText; 
[self presentModalViewController animated:YES} 

,然后在viewDidAppear:SetViewController

if(isFromNotification = YES){ 
    [self showReminders:notifText]; 
} 
1

如果我有你的权利,

后,你想显示在新视图的动画,然后显示动作片的通知?

现在你的appdelegate

[viewController showReminder:reminderText]; 

调用它的方式应该是self.viewcontroller或_viewcontroller的实际上是保留对象

在showreminder你叫

动画,这本身添加一个子视图,顺便说一句,就是在同一个线程中运行,即以串行方式运行。 ,然后再次将viewcontroller添加为子视图。 ,然后尝试将操作表从父级添加到子视图(viewcontroller),而操作表应该可能在viewcontroller本身中。

我知道了吗?

如上所述,不确定什么事情会崩溃,可能出现在几个领域。

我想: 确保您拨打通过有效的指针保留的对象(使用自例如) 有一个视图控制器,你目前模式为显示在一个单独的线程(performselectoronthread)动画子视图和对actionsheet那。 然后如果你需要打电话给家长,你设置了一个委托或者你做的丑陋的方式。

self.yourviewcontroller.myParentObj = self 

即设置yourviewcontroller指针上分的ViewController,然后你就可以公开呼吁像

[self.myParentObj whatevermethod_you_have_in_parent]; 

不过话又说回来我写这篇文章,从我的头顶..

相关问题