应用程序在后台运行时,视图消失,第一个视图控制器重新启动

时间:2015-10-25 22:02:37

标签: objective-c

在我的应用程序中有两个角色:买家和卖家,还有相应的viewcontrollers:BuyerViewController和SellerViewController。默认角色是买方,但可以从菜单注册并成为卖家。应用程序启动时,将显示BuyerViewController。买方与在线卖家开始交易。当交易开始时,买方在BuyerViewController中看到卖方的个人资料,卖方在SellerViewController中看到买方的个人资料信息。该应用程序在前台工作完美。

然而,当手机保持关闭状态2-3分钟,当我回到应用程序时,两个屏幕都会打开BuyerViewController。买家的观点清除:卖家的个人资料在买家手机中消失,而且SellerViewController完全消失。基本上两部手机都会恢复到初始状态,好像应用程序刚刚启动一样。如果应用程序在较短的时间内保持在后台,如一分钟或更短时间,则视图会保持不变。

有谁知道为什么会这样?我将不胜感激。

BuyerViewController:
    - (void)viewWillAppear:(BOOL)animated{
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate setTradeRole:@"buyer"];

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view sendSubviewToBack:mapView];

    //checking if user is registered
    PFUser *currentUser = [PFUser currentUser];
    if(currentUser.email == nil || currentUser[@"FirstName"] == nil || currentUser[@"LastName"] ==nil || currentUser[@"PictureUrl"] == nil)
    {
        UIStoryboard *LoginStoryboard = [UIStoryboard storyboardWithName:@"LoginStoryboard" bundle:nil];
        UIViewController *LoginVC = [LoginStoryboard instantiateViewControllerWithIdentifier:@"FEXLogin"];

        LoginVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

        [self presentViewController:LoginVC animated:NO completion:nil];
        return;
    }


    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    sellerArray = [[NSMutableArray alloc] init];

    [mapView  setDelegate:self];
    [locationManager setDelegate:self];

    if([[UIDevice currentDevice].systemVersion floatValue] >= 8.00){
        [locationManager requestWhenInUseAuthorization]; //needed for IOS 8
    }

    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;  //5 meters distance change will trigger update location
    [locationManager startUpdatingLocation]; 
    //Initial mapView settings
    [self.view addSubview:mapView];
    [mapView  setCenterCoordinate:self.mapView.userLocation.coordinate];
    mapView.showsUserLocation = YES;
    mapView.zoomEnabled =YES;
    mapView.scrollEnabled = YES;  
    addressField.hidden = NO;
    editBuyerAddressButton.hidden = NO;

    [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:NO]; 

    CLLocationCoordinate2D center = self.mapView.centerCoordinate;
    pinLocation = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];//necessary to assign before [getSellersLocation]

    //make sure in home page everybody is buyer

    if(currentUser)
    {
        [currentUser setObject:[NSNumber numberWithBool:NO] forKey:@"isSeller"];
        [currentUser setObject:[NSNumber numberWithBool:NO] forKey:@"isSelling"];
        [currentUser saveInBackground];
    }

    //adding the buyerPinImage

    buyerPinImage =[[UIImageView alloc] initWithFrame:CGRectMake((self.mapView.center.x-21), (self.mapView.center.y -56), 42, 56)];
    buyerPinImage.image = [UIImage imageNamed:@"buyer.png"];
    [self.view addSubview:buyerPinImage];

    __weak __typeof__(self) weakSelf = self;
    handleGeocoding = ^(NSArray *placemarks, NSError *error)
    {
        if(error == nil && [placemarks count] > 0)
        {
            weakSelf.placemark = [placemarks lastObject];

            weakSelf.addressField.hidden = weakSelf.navigationItem.rightBarButtonItem ? YES : NO;
            weakSelf.addressField.text = [NSString stringWithFormat:@"   %@ %@ %@ %@",  weakSelf.placemark.thoroughfare,  weakSelf.placemark.subThoroughfare, weakSelf.placemark.postalCode, weakSelf.placemark.locality];
            NSString *undesired = @"(null)";
            NSString *desired   = @"";

            weakSelf.addressField.text = [weakSelf.addressField.text stringByReplacingOccurrencesOfString:undesired
                                                                                               withString:desired];

            weakSelf.editBuyerAddressButton.hidden = NO;
        }

        if([weakSelf.addressField.text isEqualToString:@"  "])

            weakSelf.addressField.text = @"   Vent venligst";


    };

    CLLocation *plocation = [[CLLocation alloc] initWithLatitude:mapView.userLocation.coordinate.latitude longitude:mapView.userLocation.coordinate.longitude];
    [geocoder reverseGeocodeLocation:plocation completionHandler: handleGeocoding];


    [self.view addSubview:toCurrentLoc]; //enables button to current location
    [self.view addSubview:callButton]; //enables showing small container view
    [self.view addSubview:containerView]; //enables showing container view
    [self.view addSubview:paymentView]; //enables showing payment view


    navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    navTitleLabel.backgroundColor = [UIColor clearColor];
    navTitleLabel.font = [UIFont boldSystemFontOfSize:15.0];
    navTitleLabel.textAlignment = NSTextAlignmentCenter;
    navTitleLabel.textColor = [UIColor themeColor];
    navTitleLabel.text = @"";
    self.navigationItem.titleView = navTitleLabel;

    [self.navigationItem setRightBarButtonItem:nil];
    buyButton.hidden = NO;
    containerView.hidden = YES;
    callButton.hidden = YES;
    ratingView.hidden = YES;
    [hudContactSellers hide:YES];
    [mapView removeAnnotation:buyersPin];
    tradeState = NoTrade; //notrade
    contactingSellers = NO;

    [NSTimer scheduledTimerWithTimeInterval:6 target:locationManager selector:@selector(startUpdatingLocation) userInfo:nil repeats:YES];

    //Notification Settings
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenSeller:) name:@"listenSeller" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenTradeEnded:) name:@"listenTradeEnded" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listeBuyerResponded:) name:@"listenBuyerResponded" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(listenTradeCancelledBySeller:) name:@"listenTradeCancelledBySeller" object:nil];

    // Configure Star Layout
    [ratingView setStepInterval:1];
    [ratingView setValue:5];
    [ratingView setBaseColor:[UIColor grayColor]];
    [ratingView setMarkFont:[UIFont systemFontOfSize:32.0f]];

    // Make Sure Button Is Visible    
    [self.view bringSubviewToFront:buyButton];
    [self.view bringSubviewToFront:addressField];
    [self.view bringSubviewToFront:editBuyerAddressButton];

}

1 个答案:

答案 0 :(得分:0)

最后解决了这个问题。它是一个在后台运行的计时器,导致所有对象消失并重新启动第一个视图。在进入后台模式之前,计时器已停用,现在它正在完美运行。