[英]Compare instances or objects of same UIViewController class
我已經創建了兩個從UIViewController類派生的MasterViewController實例
_masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController_iPhone" bundle:nil];
// second instance with same class and duplicate nib view
_favItemMasterVC = [[MasterViewController alloc] initWithNibName:@"favMasterViewController_iPhone" bundle:nil];
MasterViewController_iPhone和favMasterViewController_iPhone視圖都相同。 現在,我要檢查當前選擇了哪個UIViewController(例如,在選項卡上)。 如何找到兩個對象之間的區別?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[_favItemListMasterVC class]] { // it is always called in both cases}
isMemberOfClass://也不起作用
如何檢查差異?
不確定我是否了解您在做什么,但是如果_favItemListMasterVC和_masterViewController指向添加到UITabBar的相同VC,則可以簡單地比較指針來檢查它
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _favItemListMasterVC)
{
//the visible view controller is _favItemListMasterVC
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (viewController == _masterViewController)
{
}
else if (viewController == _favItemMasterVC)
{
}
}
我認為您可以使用標簽來檢查哪個。 Tag是UIView的屬性。在兩個xib文件中設置標簽值。 並使用代碼檢查標簽。
要比較對象,您還可以使用:
if([viewController isEqual:_favItemMasterVC])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.