Ios5 ViewController中的控件在屏幕外发出内存警告后失去状态

Ios5 ViewController中的控件在屏幕外发出内存警告后失去状态,ios5,mapkit,storyboard,didreceivememorywarning,Ios5,Mapkit,Storyboard,Didreceivememorywarning,情况与秘书长所描述的非常相似,只是代表团似乎运作良好。我将提供有关我的代码的更多细节。我只是把不相关/琐碎的部分去掉 ReportScreen.h @interface ReportScreen : UIViewController <UIImagePickerControllerDelegate, UITextViewDelegate, MBProgressHUDDelegate, SoapDelegate, MapLocationChoiceDelegate> // ... @p

情况与秘书长所描述的非常相似,只是代表团似乎运作良好。我将提供有关我的代码的更多细节。我只是把不相关/琐碎的部分去掉

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
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
@protocol-MapLocationChoiceDelegate
-(void)位置选择:(MKPointAnnotation*)注释;
@结束
// ---
@界面映射屏幕:UIViewController
-(作废)handleLongPress:(id)发送方;
@属性(非原子,保留)NSMutableArray*注释;
@属性(非原子,保留)IBMMapView*mapView;
@属性(弱)id MapLocationChoiceDeleteGate;
@结束
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
@实现映射屏幕
@综合注释;
@综合地图视图;
@综合mapLocationChoiceDelegate;
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)viewDidLoad{
[超级视图下载];
UILongPressGestureRecognitzer*lpgr=[[UILongPressGestureRecognitzer alloc]initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration=1.0;
[self.mapView addgesturecognizer:lpgr];
[mapView addAnnotations:self.annotations];
}
-(无效)视图卸载{
[超级视频下载];
}
#pragma标记-映射处理
-(无效)handleLongPress:(id)发送方{
如果(![sender IsKindof类:[UILongPressGestureRecognitizer类]]){
回来
}
UILongPressGestureRecognizer*gr=(UILongPressGestureRecognizer*)发送方;
如果(gr.state!=UIgestureRecognitizerStateStart){
回来
}
CGPoint接触点=[gr locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate=[self.mapView convertPoint:touchPoint to CoordinateFromView:self.mapView];
MKPointAnnotation*annotation=[[MKPointAnnotation alloc]init];
annotation.coordinate=触摸地图坐标;
self.annotations=[NSMutableArray arrayWithArray:[mapView annotations]];
for(self.annotations中的id a){
if(![a iskindof类:[MKUserLocation类]]){
[地图视图删除说明:a];
}
}
[地图视图添加注释:注释];
self.annotations=[NSMutableArray arrayWithArray:[mapView annotations]];
//NSDictionary*userInfo=[NSDictionary Dictionary WithObject:annotation forKey:@“annotation”];
//[[NSNotificationCenter defaultCenter]postNotificationName:@“PositionSelected”对象:nil userInfo:userInfo];
[self.mapLocationChoiceDelegate locationselected:annotation];
NSLog(@“M:%@”,((ReportScreen*)self.maplocationChoiceDeleteGate.textView.text);
}
-(MKAnnotationView*)贴图视图:(MKMapView*)注释的贴图视图:(id)注释{
if([annotation isKindOfClass:[MKUserLocation类]]){
返回零;
}
静态NSString*AnnotationIdentifier=@“Annotation”;
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[theMapView退出队列可重用AnnotationView with Identifier:AnnotationIdentifier];
如果(!pinView){
pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:AnnotationIdentifier];
pinView.pinColor=MKPinAnnotationColorRed;
pinView.canShowCallout=是;
pinView.animatesDrop=是;
}否则{
pinView.annotation=注释;
}
返回pinView;
}
@结束
问题是:

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

问题:为什么?

是否有时您的电脑没有收到内存警告,并且它工作正常?我还在你的问题中添加了mapkit作为标记。是的,如果我没有收到内存警告,它工作得很好。问题是MapKit几乎总是让应用程序接收内存警告。感谢标签:)我开始怀疑这可能是因为我在ReportScreen::viewDidLoad和ReportScreen::isReadyToSubmit中做了所有这些事情……只是我不知道还有什么地方可以执行这些技巧。不管怎样,这里有一段视频显示了这种不良行为。它仍在处理中,但很快就会可用。我用ARC创建的几乎所有属性都是“@property(强,非原子)”。你为什么用“@property(nonatomic,retain)”?我猜是旧习惯吧。在ARC之前的日子里,我经常这样做,然后我跳进ARC和故事板。我应该试着换一下。编辑只是尝试了一下,没有任何区别…更多的编辑表示保留等于强。
@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