卸载视图的问题。应用程序崩溃了

时间:2010-12-17 10:47:09

标签: iphone

在我的申请中

它有tableview ...有两行。

  1. 日期。
  2. 市。
  3. 当选择日期...我加载了运行良好且良好的EventViewController视图...

    当回到我的tableview并选择City时,我的应用程序崩溃了

    我在控制台中看不到任何东西。它没有显示任何信息。

    怎么做......

    这是代码......

    The For implementation ......

    但是我的应用程序崩溃了......

      #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];
            self.title=@"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];
        }
    

    这就是我所做的......帮助我。

    @thanks提前。

2 个答案:

答案 0 :(得分:1)

请执行以下步骤:

1)按Build和Debug(Build - &gt; Build and Debug - Breakpoints on) 2)打开调试器 3)创建崩溃 4)现在,转到调试器并在左侧搜索黑色项目 5)按此项,您将找到应用程序崩溃的位置

如果您已经这样做了,请写下您的应用程序崩溃的位置 - 这将更容易。也许,你会发现错误。

答案 1 :(得分:1)

我认为你的问题可能是self.navigationController.delegate = self; - [EventViewController viewDidLoad]。当你点击后退按钮时,EventViewController被释放,但是navigationController仍然认为它是它的委托。因此,当它询问是否可以打开下一个视图控制器时,它会调用已释放对象上的方法并导致崩溃。