iOS视图控制器有时会在扫描蓝牙条码扫描器时加载两次旧的Web视图

时间:2015-10-08 20:37:55

标签: ios objective-c bluetooth

我正在创建一个从套接字蓝牙条码扫描器(Socket Mobile)读取数据的应用程序,并将该字符串注入Web视图。在应用程序中,我的控制器可以加载多次。这就是问题发生的地方。有时条形码扫描有时会在第二次加载时引用错误的Web视图。 (无对象参考)

我在视图控制器的.m文件中有以下内容

-(void)viewDidLoad
 {
     [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[StoreWebViewController checkAndPrependProtocolForUrl:self.storeUrl]]]];
 }

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

    ScanApi=[[ScanApiHelper alloc]init];
    [ScanApi setDelegate:self];
    [ScanApi open];
    ScanApiConsumer=[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(onBarcodeTimer:) userInfo:nil repeats:YES];

}

-(void)onBarcodeTimer: (NSTimer*)theTimer{
    if(theTimer==ScanApiConsumer){
        [ScanApi doScanApiReceive];
    }
}

 //called when scan happens
-(void) onDecodedDataResult:(long)result device:(DeviceInfo *)device decodedData:(ISktScanDecodedData*)decodedData
{
    if(SKTSUCCESS(result))
    {
        NSLog(@"SCAN: %p", self.webView);
    }

}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.webView = nil;
}

in .h(相关部分)

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) ScanApiHelper* ScanApi;
@property (strong, nonatomic) NSTimer* ScanApiConsumer;

我扫描时第一次加载:

2015-10-08 16:05:08.368 PHP POS[2646:971844] SCAN: 0x15cd3c6f0
2015-10-08 16:05:09.367 PHP POS[2646:971844] SCAN: 0x15cd3c6f0
2015-10-08 16:05:11.168 PHP POS[2646:971844] SCAN: 0x15cd3c6f0

我扫描时的第二次加载:

2015-10-08 16:05:38.670 PHP POS[2646:971844] SCAN: 0x15ee5ddb0
2015-10-08 16:05:40.269 PHP POS[2646:971844] SCAN: 0x15ee5ddb0
2015-10-08 16:05:41.168 PHP POS[2646:971844] SCAN: 0x0 //OLD WEB VIEW
2015-10-08 16:05:43.269 PHP POS[2646:971844] SCAN: 0x15ee5ddb0
2015-10-08 16:05:44.368 PHP POS[2646:971844] SCAN: 0x0 //OLD WEB VIEW

有没有办法解决这个问题?我不确定它是如何引用这两个对象的......计时器似乎没有运行两次;因为我扫描并且只获得一个NSLog条目

编辑:

以下是我创建StoreWebViewController的方法;这可能会发生多次。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"continueToStore"])
    {
        StoreWebViewController *controller = (StoreWebViewController *)segue.destinationViewController;
        controller.storeUrl = [StoreUrlViewController checkAndPrependProtocolForStoreUrl:self.storeUrl.text];
    }
}

1 个答案:

答案 0 :(得分:0)

问题甚至在视图被破坏后,前一个对象的计时器仍在运行。将以下代码添加到viewDidDisappear修复它

table.invisible
{
     margin: 0 auto;
}
table.invisible td
{
     border: 0;
}