0
使用以下簡單佈局。我有一個UINavigation Controller,帶有一個根視圖控制器。從根視圖控制器我有兩個其他視圖控制器可以從根視圖控制器推送。當它接近iBeacon時,我的應用程序能夠通過控制器推送某個控制器。雖然iBeacon是無關緊要的,但我遇到的問題有時是相同的View Controller在它已經顯示時會被推入,導致崩潰。如何停止在自己的頂部推送一個UIViewController?
我該如何預防?我想確保某個View Controller是否已經被推送,而不是再次推送它,但是,如果需要推送另一個View Controller,那麼彈出一個不是根視圖控制器的推送控件,然後推送它。
錯誤消息:
2014-02-27 11:16:08.038 Gimbal Proximity[3397:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'
*** First throw call stack:
(0x3028ee83 0x3a5eb6c7 0x3028edc5 0x32a1ea67 0x32a1e9f3 0x32be74f9 0x32a24cf5 0x32be6d4d 0x32ba43e9 0x32ac1a17 0x32ac1821 0x32ac17b9 0x32a13353 0x32699943 0x32695167 0x32694ff9 0x32694a0d 0x3269481f 0x3268e54d 0x30259f69 0x302578f7 0x30257c43 0x301c2471 0x301c2253 0x34efc2eb 0x32a77845 0xe7489 0x3aae4ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
信標1是根視圖控制器。
這是從我的根視圖控制器推視圖控制器的代碼。
NSArray* views = [self.navigationController viewControllers];
NSLog(@"views %@", views);
if ([visit.transmitter.name isEqualToString:@"Beacon1"])
{
NSLog(@"Welcome to Beacon 1");
NSLog(@"Beacon 1 %@", self.navigationController.topViewController);
[self showLabels];
[self BeaconOne:@"10"];
}
else if ([visit.transmitter.name isEqualToString:@"Beacon2"])
{
NSLog(@"Welcome to Beacon 2");
ProductViewController *product_VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProductViewController"];
product_VC.productNumber = @"0234609";
[self.navigationController pushViewController:product_VC animated:YES];
NSLog(@"Beacon 2 %@", self.navigationController.topViewController);
}
else if ([visit.transmitter.name isEqualToString:@"Beacon3"])
{
NSLog(@"Welcome to Beacon 3");
CurrentPromotionsViewController *currentPromotions_VC = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentPromotionsViewController"];
currentPromotions_VC.storeNumber = 10;
currentPromotions_VC.productCountryOrigin = @"France";
currentPromotions_VC.productCategory = @"Wine";
[self.navigationController pushViewController:currentPromotions_VC animated:YES];
NSLog(@"Beacon 3 %@", self.navigationController.topViewController);
}