1

我正在研究估计信标。

当我在 switch Case“立即”中时,我尝试呈现一个 ViewController。但是当我加载视图时,我有一个警告:

2014-03-13 02:44:26.017 ProximityDemo[856:60b] 警告:尝试呈现不在窗口层次结构中的视图!

为什么 ?我认为当我在新视图上时,presentView 方法仍在工作。

此外,当我在新视图上时,我想在我处于“附近”的情况下弹出到旧视图

我想我必须在新的 ViewController 中实现所有代码?(presentProductViewController) 有没有办法让所有的接近/距离控制只在一个控制器中?

这是我的代码:

ESTViewController:

#import "ESTViewController.h"
#import <ESTBeaconManager.h>
#import "PresentProductViewController.h"

@interface ESTViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) ESTBeacon* selectedBeacon;

@end

@implementation ESTViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

/////////////////////////////////////////////////////////////
// setup Estimote beacon manager

// craete manager instance
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;
self.beaconManager.avoidUnknownStateBeacons = YES;

// create sample region object (you can additionaly pass major / minor values)
ESTBeaconRegion* region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_IOSBEACON_PROXIMITY_UUID
                                                              identifier:@"EstimoteSampleRegion"];

// start looking for estimote beacons in region
// when beacon ranged beaconManager:didRangeBeacons:inRegion: invoked
[self.beaconManager startRangingBeaconsInRegion:region];



}

-(void)beaconManager:(ESTBeaconManager *)manager
 didRangeBeacons:(NSArray *)beacons
        inRegion:(ESTBeaconRegion *)region
{
if([beacons count] > 0)
{

    if(!self.selectedBeacon)
    {
        // initialy pick closest beacon
        self.selectedBeacon = [beacons objectAtIndex:0];
    }
    else
    {
        for (ESTBeacon* cBeacon in beacons)
        {
            // update beacon it same as selected initially
            if([self.selectedBeacon.major unsignedShortValue] == [cBeacon.major unsignedShortValue] &&
               [self.selectedBeacon.minor unsignedShortValue] == [cBeacon.minor unsignedShortValue])
            {
                self.selectedBeacon = cBeacon;
            }
        }
    }



    // beacon array is sorted based on distance
    // closest beacon is the first one

    NSString* labelText = [NSString stringWithFormat:
                           @"Major: %i, Minor: %i\nRegion: ",
                           [self.selectedBeacon.major unsignedShortValue],
                           [self.selectedBeacon.minor unsignedShortValue]];

    // calculate and set new y position
    switch (self.selectedBeacon.proximity)
    {
        case CLProximityUnknown:
        {
            labelText = [labelText stringByAppendingString: @"Unknown"];
            break;
        }
        case CLProximityImmediate:
        {
            labelText = [labelText stringByAppendingString: @"Immediate"];

            PresentProductViewController *showViewController = [[PresentProductViewController alloc] initWithNibName:@"PresentProductViewController" bundle:nil];

            [self presentViewController:showViewController animated:YES completion:nil];

            break;
        }
        case CLProximityNear:
        {
            labelText = [labelText stringByAppendingString: @"Near"];
            break;

            //[self.navigationController popToRootViewControllerAnimated:YES];
            //ESTViewController *initViewController = [[ESTViewController alloc]init];
            //[self presentViewController:initViewController animated:YES completion:nil];
        }
        case CLProximityFar:
        {
            labelText = [labelText stringByAppendingString: @"Far"];
            break;
        }

        default:
            break;
    }

    self.distanceLabel.text = labelText;
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

当前产品视图控制器:

#import "PresentProductViewController.h"
#import <ESTBeaconManager.h>
#import "ESTViewController.h"

@interface PresentProductViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) ESTBeacon* selectedBeacon;

@end

@implementation PresentProductViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.activityIndicator startAnimating];

/////////////////////////////////////////////////////////////
// setup Estimote beacon manager

// craete manager instance
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;
self.beaconManager.avoidUnknownStateBeacons = YES;

// create sample region object (you can additionaly pass major / minor values)
ESTBeaconRegion* region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_IOSBEACON_PROXIMITY_UUID
                                                              identifier:@"EstimoteSampleRegion"];

// start looking for estimote beacons in region
// when beacon ranged beaconManager:didRangeBeacons:inRegion: invoked
[self.beaconManager startRangingBeaconsInRegion:region];


}

-(void)beaconManager:(ESTBeaconManager *)manager
 didRangeBeacons:(NSArray *)beacons
        inRegion:(ESTBeaconRegion *)region
{
if([beacons count] > 0)
{

    if(!self.selectedBeacon)
    {
        // initialy pick closest beacon
        self.selectedBeacon = [beacons objectAtIndex:0];
    }
    else
    {
        for (ESTBeacon* cBeacon in beacons)
        {
            // update beacon it same as selected initially
            if([self.selectedBeacon.major unsignedShortValue] == [cBeacon.major unsignedShortValue] &&
               [self.selectedBeacon.minor unsignedShortValue] == [cBeacon.minor unsignedShortValue])
            {
                self.selectedBeacon = cBeacon;
            }
        }
    }



    // beacon array is sorted based on distance
    // closest beacon is the first one

    self.labelText.text = [NSString stringWithFormat:
                           @"Major: %i, Minor: %i\nRegion: ",
                           [self.selectedBeacon.major unsignedShortValue],
                           [self.selectedBeacon.minor unsignedShortValue]];

    // calculate and set new y position
    switch (self.selectedBeacon.proximity)
    {
        case CLProximityUnknown:
        {
            self.labelText.text = [self.labelText.text stringByAppendingString: @"Unknown"];
            break;
        }
        case CLProximityImmediate:
        {
            self.labelText.text = [self.labelText.text stringByAppendingString: @"Immediate"];
            break;
        }
        case CLProximityNear:
        {
            self.labelText.text = [self.labelText.text stringByAppendingString: @"Near"];
            break;

            //UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
            //UIViewController *initViewController = [storyBoard instantiateInitialViewController];

            //[self.navigationController pushViewController:initViewController animated:YES];

            //ESTViewController *initViewController = [[ESTViewController alloc]init];
            //[self presentViewController:initViewController animated:YES completion:nil];

            //[self.navigationController popToRootViewControllerAnimated:YES];

        }
        case CLProximityFar:
        {
            self.labelText.text = [self.labelText.text stringByAppendingString: @"Far"];
            break;
        }

        default:
            break;
    }

}
}

-(void)viewDidDisappear:(BOOL)animated
{
[self.activityIndicator stopAnimating];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

我确定我做错了。谢谢你的帮助。

4

1 回答 1

1

即使您已经ESTBeaconManager提交了. 我认为会发生以下情况:didRangeBeacons:inRegionPresentProductViewController

  1. ESTViewController'sdidRangeBeacons:inRegion是用非空的信标数组调用的。
  2. 所选信proximity标的属性为CLProximityImmediate,并PresentProductViewController以模态方式呈现。
  3. 在呈现(呈现)时,viewcontrollerESTViewControllerdidRangeBeacons:inRegion被调用并进行新的更改。同样,信标具有立即邻近性,并且以PresentProductViewController模态方式呈现的新实例。

来自ViewController 编程指南

任何视图控制器对象一次都可以呈现一个视图控制器。

如果上面概述的假设是正确的,那么您正在尝试同时以模态方式呈现多个视图控制器,这不是一个有效的操作。您可以尝试在视图消失时停止对信标的测距,然后在它出现时再次开始测距。这将阻止信标管理器在ESTViewController无法呈现视图控制器时通知更改。实现此目的的覆盖viewWillDisappear和方法:viewWillAppear

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.beaconManager stopRangingBeaconsInRegion:region];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.beaconManager startRangingBeaconsInRegion:region];
}
于 2014-03-13T21:15:03.490 回答