2012-01-05 32 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]; 

不過話又說回來我寫這篇文章,從我的頭頂..

相關問題