2012-01-05 14 views
5

में दूसरे दृश्य दृश्य नियंत्रक से किसी विधि के कॉल पर फर्स्ट व्यू कंट्रोलर दिखाने के लिए वास्तव में मैं अलार्म ऐप बना रहा हूं। उस समय जब मैं समय निर्धारित करता हूं तो UILocalNotification ईवेंट उस समय होता है और यह AppDelegate क्लास की विधि को कॉल करता है ieeReceiveNotifications विधि। इस विधि में मैंने SetViewController (showReminder विधि) की विधि को कॉल करने के लिए एक कोड लिखा है और अब इस विधि में मैं चाहता हूं कि इसे एक नया व्यू कंट्रोलर i.e TimeViewController दिखाना चाहिए क्योंकि अलार्म आमंत्रण के दौरान मुझे एनीमेशन दिखाना है।आईफोन

मुझे इसकी आवश्यकता है जब अलार्म ने आमंत्रित किया है कि मैंने एक एक्शन शीट दिखाई देने के लिए सेट किया है, लेकिन मैं एनीमेशन भी दिखाना चाहता हूं। सभी दृश्यों में एक्शन शीट दिखाई देती है लेकिन एनीमेशन केवल विशेष दृश्य में दिखाया जा सकता है, इसलिए मुझे दिखाने की ज़रूरत है एक अलग 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]; –

+0

हल करना चाहिए कोई दोस्त नहीं ... इसकी नहीं .. मैंने सभी किस्मों की कोशिश की है: पी जैसे PresentModalViewController, dismissModalViewController, AddSubview, superView को हटाएं ... लेकिन नकारात्मक परिणाम। :( – mAc

+0

आप में ऐप डिलीगेट करें, क्या आपका विचार नियंत्रक पहले से ही आवंटित और inited है? – Canopus

उत्तर

1

आपका ViewController शायद दृश्य दिखा रहा है, लेकिन स्क्रीन पर SetViewController के दृश्य के बिना आप इसे नहीं देख सकते हैं। आपको पहले SetViewController पर जाना होगा और फिर अपना टाइम व्यू कंट्रोलर प्रस्तुत करना होगा। क्या यह सही है, आप SetViewController दिखाना चाहते हैं लेकिन शो रेमिन्डर को कॉल करें: विधि तुरंत? लेकिन केवल didReceiveLocalNotification से:।

यदि यह मामला है, एक झंडा

BOOL isFromNotification; 
NSString *notifText; 

निर्धारित करते हैं, और अपने SetViewControllers में एक पाठ संपत्ति ज, और 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]; 

से फोन जो जिस तरह से आप कॉल

एनीमेशन, जो अपने आप में

showreminder में वास्तव में बनाए रखा वस्तु के लिए self.viewcontroller या _viewcontroller होना चाहिए एक सबव्यूव जोड़ता है और वैसे ही उसी धागे में चल रहा है, यानी धारावाहिक में। और फिर आप व्यू कंट्रोलर को एक सबव्यू के रूप में दोबारा जोड़ते हैं। और फिर आप वर्कशीट को पैरेंट से सबव्यू (व्यू कंट्रोलर) में जोड़ने का प्रयास करते हैं, जब वर्कशीट शायद व्यू कंट्रोलर में ही होनी चाहिए।

क्या मुझे यह अधिकार मिला?

कोई निश्चित रूप से वास्तव में क्या टूट रहा है, ऊपर बताए गए कई क्षेत्रों पर हो सकता है।

मैं होगा: सुनिश्चित करें कि आप वैध संकेत के माध्यम से बनाए रखा वस्तुओं (उदाहरण के लिए स्वयं का प्रयोग करके) फोन कर एक ViewController है कि आप एक subview कि एक अलग थ्रेड (performselectoronthread) पर एनिमेशन दिखा के रूप में उपस्थित मोडल और actionsheet पर है उस। फिर यदि आपको माता-पिता को कॉल करने की आवश्यकता है, तो आप एक प्रतिनिधि सेट अप करते हैं या आप बदसूरत तरीके से करते हैं।

self.yourviewcontroller.myParentObj = self 

यानी उप ViewController, जिसे फिर आप खुले तौर पर की तरह

[self.myParentObj whatevermethod_you_have_in_parent]; 

लेकिन तब फिर मैं अपने सिर के ऊपर से इस बारे में फोन कर सकते हैं पर एक yourviewcontroller सूचक सेट ..