2010-12-17 48 views
0

在我的應用程序卸載視圖的問題。應用程序崩潰

它確實有tableview ...有兩行。

  1. 日期。
  2. 城市。

當選擇日期...我加載它運作良好,良好的EventViewController視圖...

又回到了我的tableview時

和選擇的城市我的應用程序獲取墜毀

我沒有看到任何東西在我的控制檯。它沒有顯示任何消息。

怎麼辦......

這裏是代碼....

的爲了實現......

,但我的應用程序崩潰得到...

#import "SearchViewController.h" 
    #import "SearchDetailViewController.h" 
    #import "EventViewController.h" 

    @implementation SearchViewController 
    @synthesize searchListArray; 
    /* 
    // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
     if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
      // Custom initialization 
     } 
     return self; 
    } 
    */ 


    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; 
     self.navigationItem.backBarButtonItem=barButton; 
     [barButton release]; 
     searchListArray =[[NSMutableArray alloc]init]; 
     [searchListArray addObject:@"City"]; 
     [searchListArray addObject:@"Populair"]; 
     [searchListArray addObject:@"Date"]; 
    } 

    - (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 [searchListArray count]; 
    } 

     // 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]; 
      cell.selectionStyle= UITableViewCellSelectionStyleBlue; 
    //  cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
      cell.backgroundColor=[UIColor blueColor]; 
      cell.textLabel.text=[searchListArray objectAtIndex:indexPath.row]; 
      switch (indexPath.row) { 
       case 0: 
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
        break; 
        case 2: 
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
        break; 

       default: 
        break; 

      } 
     } 
     return cell; 

    } 
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

     switch (indexPath.row) { 
      case 0: 
       NSLog(@"searchdetailedview"); 
       SearchDetailViewController *searchDetailViewController = [[SearchDetailViewController alloc] initWithNibName:@"SearchDetailViewController" bundle:nil]; 
       [self.navigationController pushViewController:searchDetailViewController animated:YES]; 
        // self.navigationItem.title = @"Back"; 
       [searchDetailViewController release]; 
       searchDetailViewController=nil; 
       break; 
       case 2: 
       NSLog(@"eventviewController"); 

       EventViewController *eventViewController=[[EventViewController alloc]initWithNibName:@"EventViewController" bundle:nil]; 
       [self.navigationController pushViewController:eventViewController animated:YES]; 
       [eventViewController release]; 
       eventViewController=nil; 
       break; 
      default: 
       break; 
     } 
    } 



    /* 
    // Override to allow orientations other than the default portrait orientation. 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     // Return YES for supported orientations 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 
    */ 



    - (void)didReceiveMemoryWarning { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Release any cached data, images, etc that aren't in use. 
    } 

    - (void)viewDidUnload { 
     [super viewDidUnload]; 
     // Release any retained subviews of the main view. 
     // e.g. self.myOutlet = nil; 
    } 


    - (void)dealloc { 
     [searchListArray release]; 
     [super dealloc]; 
    } 

EventViewController

#import "EventViewController.h" 

    @implementation EventViewController 
    @synthesize eventsList; 
    @synthesize eventStore; 
    @synthesize defaultCalendar; 
    @synthesize detailViewController; 

    #pragma mark - 
    #pragma mark View lifecycle 


    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     [email protected]"Event List"; 
     self.eventStore=[[EKEventStore alloc]init]; 
     self.eventsList=[[NSMutableArray alloc]initWithArray:0]; 
     self.defaultCalendar=[self.eventStore defaultCalendarForNewEvents]; 
      // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: 
              UIBarButtonSystemItemAdd target:self action:@selector(addEvent:)]; 
     self.navigationItem.rightBarButtonItem = addButtonItem; 
     [addButtonItem release]; 
     self.navigationController.delegate = self; 
     [self.eventsList addObjectsFromArray:[self fetchEventsForToday]]; 
     [self.tableView reloadData]; 
    } 

    - (IBAction) addEvent:(id)sender{ 
     EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil]; 
      // set the addController's event store to the current event store. 
     addController.eventStore = self.eventStore; 
      // present EventsAddViewController as a modal view controller 
     [self presentModalViewController:addController animated:YES]; 
     addController.editViewDelegate = self; 
     [addController release]; 
    } 

    - (NSArray *) fetchEventsForToday{ 

     NSDate *startDate = [NSDate date]; 
      // endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate 
     NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400]; 
      // Create the predicate. Pass it the default calendar. 
     NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar]; 
     NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate 
                     calendars:calendarArray]; 
      // Fetch all events that match the predicate. 
     NSArray *events = [self.eventStore eventsMatchingPredicate:predicate]; 
     return events; 
    } 

    - (void)viewWillAppear:(BOOL)animated { 
      // [super viewWillAppear:animated]; 
     [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:NO]; 

    } 


    /* 
    - (void)viewDidAppear:(BOOL)animated { 
     [super viewDidAppear:animated]; 
    } 
    */ 
    /* 
    - (void)viewWillDisappear:(BOOL)animated { 
     [super viewWillDisappear:animated]; 
    } 
    */ 
    /* 
    - (void)viewDidDisappear:(BOOL)animated { 
     [super viewDidDisappear:animated]; 
    } 
    */ 

    // Override to allow orientations other than the default portrait orientation. 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
     // Return YES for supported orientations 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 



    #pragma mark - 
    #pragma mark Table view data source 

    //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // // Return the number of sections. 
    // // return <#number of sections#>; 
    //} 


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     // Return the number of rows in the section. 
     return [eventsList count]; 
    } 


    // Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCellAccessoryType editableCellAccessoryType =UITableViewCellAccessoryDisclosureIndicator; 


     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     cell.accessoryType=editableCellAccessoryType; 
     cell.textLabel.text=[[self.eventsList objectAtIndex:indexPath.row]title]; 
     // Configure the cell... 

     return cell; 
    } 


    /* 
    // Override to support conditional editing of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
     // Return NO if you do not want the specified item to be editable. 
     return YES; 
    } 
    */ 


    /* 
    // Override to support editing the table view. 
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      // Delete the row from the data source 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     else if (editingStyle == UITableViewCellEditingStyleInsert) { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
     } 
    } 
    */ 


    /* 
    // Override to support rearranging the table view. 
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
    } 
    */ 


    /* 
    // Override to support conditional rearranging of the table view. 
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
     // Return NO if you do not want the item to be re-orderable. 
     return YES; 
    } 
    */ 


    #pragma mark - 
    #pragma mark Table view delegate 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic may go here. Create and push another view controller. 
    // Upon selecting an event, create an EKEventViewController to display the event. 
     self.detailViewController = [[EKEventViewController alloc] initWithNibName:nil bundle:nil];   
     detailViewController.event = [self.eventsList objectAtIndex:indexPath.row]; 
    // Allow event editing. 
     detailViewController.allowsEditing = YES; 
    // Push detailViewController onto the navigation controller stack 
    // If the underlying event gets deleted, detailViewController will remove itself from 
    // the stack and clear its event property. 
     [self.navigationController pushViewController:detailViewController animated:YES]; 

    } 


    #pragma mark - 
    #pragma mark Navigation Controller delegate 

    - (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ 
     if (viewController == self && self.detailViewController.event.title == NULL) { 
      [self.eventsList removeObject:self.detailViewController.event]; 
      [self.tableView reloadData]; 
     } 
    } 

    - (void)eventEditViewController:(EKEventEditViewController *)controller 
       didCompleteWithAction:(EKEventEditViewAction)action{ 
     NSError *error=nil; 
     EKEvent *thisEvent=controller.event; 
     switch (action) { 
      case EKEventEditViewActionCanceled: 
       break; 
      case EKEventEditViewActionSaved: 
       if (self.defaultCalendar ==thisEvent.calendar) { 
        [self.eventsList addObject:thisEvent]; 
       } 
       [controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error]; 
       [self.tableView reloadData]; 
       break; 
      case EKEventEditViewActionDeleted: 
       if (self.defaultCalendar == thisEvent.calendar) { 
        [self.eventsList removeObject:thisEvent]; 
       } 
       [controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error]; 
       [self.tableView reloadData]; 
       break; 

      default: 
       break; 
     } 
     [controller dismissModalViewControllerAnimated:YES]; 
    } 

    - (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller{ 
     EKCalendar *calendarForEdit=self.defaultCalendar; 
     return calendarForEdit; 
    } 
    #pragma mark - 
    #pragma mark Memory management 

    - (void)didReceiveMemoryWarning { 
     // Releases the view if it doesn't have a superview. 
     [super didReceiveMemoryWarning]; 

     // Relinquish ownership any cached data, images, etc that aren't in use. 
    } 

    - (void)viewDidUnload { 
     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
     // For example: self.myOutlet = nil; 
     self.eventsList = nil; 
     [self.detailViewController release]; 

    } 


    - (void)dealloc { 
     [super dealloc]; 
    } 

這就是我所做的....幫助我。

@提前感謝。

+0

您的表格現在是自由對象或您的視圖是自由對象。請給出一些代碼來理解問題。 – Ishu 2010-12-17 11:02:26

回答

1

請執行下列操作步驟:

1)按構建和調試(建設 - >構建和調試 - 斷點上) 2)打開你的調試器 3)創建崩潰 4)現在,到調試器和搜索左側的黑色項目 5)按​​此項目,你會發現應用程序崩潰的地方

如果你已經完成了這個工作,請寫下你的應用程序崩潰的地方 - 這將會容易得多。也許,你會發現錯誤。

1

我認爲你的問題可能是在 - [EventViewController viewDidLoad]中的self.navigationController.delegate = self;。當你點擊後退按鈕時,EventViewController被釋放,但navigationController仍然認爲它是它的代表。因此,當它詢問是否可以打開下一個視圖控制器時,它會調用釋放對象上的方法並導致崩潰。

+0

@ughovgfhw ...感謝重播...... Yeap其工作完美非常感謝。 – 2010-12-18 06:34:53

相關問題