2012-01-05 12 views
5

Thực ra tôi đang tạo một ứng dụng Alarm. Trong đó Khi tôi thiết lập thời gian Sự kiện UILocalNotification xảy ra tại thời điểm đó và nó gọi phương thức của lớp AppDelegate tức là phương thức didReceiveNotifications. Trong phương pháp này tôi đã viết một mã để gọi một phương pháp của SetViewController (phương pháp showReminder) và bây giờ trong phương pháp này tôi muốn rằng nó sẽ hiển thị một NewViewController i.e TimeViewController như tôi có để hiển thị hình ảnh động khi báo động Invokes.Để hiển thị A FirstViewController On Call của một phương thức từ SecondViewController trong Iphone

Tôi cần thông báo này khi Khi báo thức gọi tôi đã đặt một trang Hành động xuất hiện nhưng tôi muốn hiển thị ảnh động. Một trang xuất hiện trong tất cả các chế độ xem Nhưng hoạt ảnh có thể được hiển thị ở chế độ xem cụ thể, đó là lý do tôi cần hiển thị Một ViewController khác.

Đây là mã cho những gì tôi đang cố gắng: -? Tôi đã thử al này cũng như PresentModalViewController, dismissModalViewController, addSubview, loại bỏ SuperView ... nhưng kết quả là âm :(gì tôi nên làm gì ..

Hầu như toàn bộ mã: -

appdelegate class: -

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

xin lỗi nếu tôi không có bạn nhưng [self.view removeFromSubview]; nên giải quyết –

+0

Không có dude ... Không phải của nó .. Tôi đã thử tất cả các giống: P như PresentModalViewController, dismissModalViewController, AddSubview, loại bỏ superView ... nhưng kết quả tiêu cực. :( – mAc

+0

Trong appDelegate, viewController của bạn đã được cấp phát và đã bắt đầu chưa? – Canopus

Trả lời

1

viewController của bạn có thể hiển thị chế độ xem nhưng không có chế độ xem của SetViewController trên màn hình, bạn không thể nhìn thấy nó. Trước tiên bạn sẽ phải đi đến SetViewController và sau đó trình bày TimeViewController của bạn. Phải không, bạn muốn hiển thị SetViewController nhưng gọi phương thức showReminder: ngay lập tức? Nhưng chỉ từ didReceiveLocalNotification :.

Nếu đây là trường hợp, thiết lập một lá cờ, và một tài sản văn bản trong SetViewControllers bạn .h,

BOOL isFromNotification; 
NSString *notifText; 

và trình bày các SetViewController, và đặt cờ

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

và sau đó trong viewDidAppear: của SetViewController

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

Nếu tôi có bạn đúng,

khi thông báo bạn muốn hiển thị hình ảnh động trên một cái nhìn mới và sau đó hiển thị bảng hành động?

Ngay bây giờ bạn gọi từ appdelegate

[viewController showReminder:reminderText]; 

mà bằng cách này nên self.viewcontroller hoặc _viewcontroller cho đối tượng thực sự giữ lại

Trong showreminder bạn gọi

hoạt hình, mà tự nó thêm một subview và bằng cách đang chạy trong cùng một thread, tức là trong serial. và sau đó bạn thêm lại bộ điều khiển xem dưới dạng một chế độ xem phụ. và sau đó bạn cố gắng thêm actionheet từ cha mẹ vào subview (viewcontroller), khi actionheet có lẽ nên ở trong viewcontroller chính nó.

Tôi có đúng không?

Không chắc chắn những gì đang bị hỏng, có thể ở một số khu vực, như được chỉ ra ở trên.

tôi sẽ: chắc chắn rằng bạn gọi các đối tượng giữ lại thông qua con trỏ hợp lệ (có sử dụng ví dụ) có một viewController mà bạn phương thức trình bày như là một subview cho thấy hình ảnh động trên một sợi riêng biệt (performselectoronthread) và có actionsheet trên cái đó. Sau đó, nếu bạn cần phải gọi cho phụ huynh, bạn thiết lập một đại biểu hoặc bạn làm theo cách xấu xí.

self.yourviewcontroller.myParentObj = self 

tức là thiết lập một con trỏ yourviewcontroller trên viewController phụ, mà bạn có thể sau đó công khai gọi như

[self.myParentObj whatevermethod_you_have_in_parent]; 

Nhưng sau đó một lần nữa tôi viết những dòng này từ đỉnh đầu của tôi ..