在屏幕外的内存警告后,ViewController中的控件丢失状态

时间:2012-02-20 18:19:53

标签: ios5 mapkit storyboard didreceivememorywarning

情况与this my other question描述的情况非常相似,只是代表团似乎工作正常。我正在提供有关我的代码的更多细节。我只是在删除不相关/无关紧要的部分。

ReportScreen.h

@interface ReportScreen : UIViewController <UIImagePickerControllerDelegate, UITextViewDelegate, MBProgressHUDDelegate, SoapDelegate, MapLocationChoiceDelegate>
// ...
@property (nonatomic, retain) MKPointAnnotation *annotation;
@property (nonatomic, retain) IBOutlet UITextView *textView;
@property (nonatomic, retain) IBOutlet UIButton *cameraButton;
@property (nonatomic, retain) IBOutlet UIButton *libraryButton;
@property (nonatomic, retain) IBOutlet UIButton *locationButton;
@property (nonatomic, retain) IBOutlet UIButton *sendButton;

@end

ReportScreen.m

@implementation ReportScreen

@synthesize annotation;
@synthesize textView;
@synthesize cameraButton;
@synthesize libraryButton;
@synthesize locationButton;
@synthesize sendButton;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Here I used to store the VC's state to a file but it shouldn't be needed now that I'm assigning it as delegate and said delegate seems to still be there even after a memory warning.
}

- (void)viewDidLoad {
    [super viewDidLoad];

    placeholderText = @"Tell us what's wrong…";
    textView.text = placeholderText;
    self.annotation = nil;
    [self isReadyToSubmit];

    hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:hud];
    hud.delegate = self;
    hud.labelText = @"Invio in corso…";
    hud.dimBackground = YES;
}

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

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Here I used to restore the state of the VC from file but… y'know.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self isReadyToSubmit];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"goToMap"]) {
        MapScreen *vc = (MapScreen *)segue.destinationViewController;

// HERE's the magic
        vc.mapLocationChoiceDelegate = self;
// MAGIC ends

        if(self.annotation != nil) {
            vc.annotations = [[NSMutableArray alloc] init];
            [vc.annotations addObject:self.annotation];
        }
    }
}

- (BOOL)isReadyToSubmit {
    if(self.annotation != nil) {
        locationButton.highlighted = YES;
    }
    if(![textView.text isEqualToString:placeholderText] && self.annotation != nil) {
        [sendButton setEnabled:YES];
    } else {
        [sendButton setEnabled:NO];
    }
    return [sendButton isEnabled];
}

- (void)textViewDidBeginEditing:(UITextView *)theTextView {
    if([theTextView.text isEqualToString:placeholderText]) {
        theTextView.text = @"";
    }
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(didFinishEditing:)];
    [self.navigationItem setRightBarButtonItem:done animated:YES];
}

- (void)textViewDidEndEditing:(UITextView *)theTextView {
    if([theTextView.text isEqualToString:@""]) {
        theTextView.text = placeholderText;
    }
    [self isReadyToSubmit];
}

- (void)didFinishEditing:(id)sender {
    [self.navigationItem setRightBarButtonItem:nil animated:YES];
    [self.textView resignFirstResponder];
}

// THIS is my delegate protocol's method
- (void)locationChosen:(MKPointAnnotation *)theAnnotation {
    self.annotation = theAnnotation;
    NSLog(@"R: %@", textView.text);
}

@end

MapScreen.h

@protocol MapLocationChoiceDelegate <NSObject>
- (void)locationChosen:(MKPointAnnotation *)annotation;
@end

// ---

@interface MapScreen : UIViewController <MKMapViewDelegate>

- (void)handleLongPress:(id)sender;

@property (nonatomic, retain) NSMutableArray *annotations;
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@property (weak) id<MapLocationChoiceDelegate> mapLocationChoiceDelegate;

@end

MapScreen.m

@implementation MapScreen

@synthesize annotations;
@synthesize mapView;
@synthesize mapLocationChoiceDelegate;

- (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)viewDidLoad {
    [super viewDidLoad];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;
    [self.mapView addGestureRecognizer:lpgr];

    [mapView addAnnotations:self.annotations];
}

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


#pragma mark - Map handling

- (void)handleLongPress:(id)sender {
    if(![sender isKindOfClass:[UILongPressGestureRecognizer class]]) {
        return;
    }

    UILongPressGestureRecognizer *gr = (UILongPressGestureRecognizer *)sender;
    if (gr.state != UIGestureRecognizerStateBegan) {
        return;
    }

    CGPoint touchPoint = [gr locationInView:self.mapView];   
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = touchMapCoordinate;
    self.annotations = [NSMutableArray arrayWithArray:[mapView annotations]];
    for(id a in self.annotations) {
        if(![a isKindOfClass:[MKUserLocation class]]) {
            [mapView removeAnnotation:a];
        }
    }
    [mapView addAnnotation:annotation];
    self.annotations = [NSMutableArray arrayWithArray:[mapView annotations]];
    // NSDictionary *userInfo = [NSDictionary dictionaryWithObject:annotation forKey:@"annotation"];
    // [[NSNotificationCenter defaultCenter] postNotificationName:@"PositionChosen" object:nil userInfo:userInfo];
    [self.mapLocationChoiceDelegate locationChosen:annotation];
    NSLog(@"M: %@", ((ReportScreen *)self.mapLocationChoiceDelegate).textView.text);
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    static NSString *AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView) {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } else {
        pinView.annotation = annotation;
    }
    return pinView;
}

@end

问题是:

  • ReportScreen推送(实际上是对MapScreen执行segue。
  • 如果我在UITextView中有一些数据,或者我将某些状态设置为ReportScreen中的按钮,并且在推送MapScreen时出现内存警告,那么一旦我返回到ReportScreen,所有这些字段都不会显示那些设置。显然仍然设置了textView.text,按钮的状态也是如此,它们只是没有显示。

问题:为什么?

0 个答案:

没有答案