2013-12-17 61 views
0

檢查我的UIView是否已經顯示在屏幕上的最佳方法是什麼?我有UISplitViewController,我試圖在點擊tableViewCell後在自定義UIView中顯示細節。我想要做的就是避免重複已經顯示的視圖,或者如果可能的話關閉現在並顯示新的視圖。檢查我的UIView是否已被顯示的最佳方法是什麼?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSLog(@"did select row at index path %ld",(long)indexPath.row); 
InvoiceDetailsVC *detailsVC = [[InvoiceDetailsVC alloc]initWithFrame:CGRectMake(321, 0,708, 709)]; 

//here I would like to check if my detailsVC has already been shown 
//if not I would like to display in that way 
[self.splitViewController.view addSubview:detailsVC]; 
} 

感謝所有幫助 問候

回答

0

這是做到這一點的最佳方式..

if (_pageViewController.isViewLoaded && _pageViewController.view.window) 
{ 
    NSLog(@"View is on screen"); 
} 

因爲view.window只有當視圖出現在屏幕上的價值。

+0

它不適合我,因爲我嘗試加載UIView而不是UIViewController。 InvoiceDetailsVC是一個自定義的uiview。所以這個方法對我的自定義uiview不可見。在我的故事板中,我將UISplitViewController添加爲UITabBarController的tabBarItem,並且因爲我無法與detailsViewController進行通信,因爲splitviewcontroleller必須位於應用程序的根目錄。我嘗試將選定的UITAbleviewCell的細節顯示爲splitviewcontroller的子視圖。我的問題還有其他解決方案嗎? – sonoDamiano

0

另一種選擇是覆蓋willMoveToSuperview:保留這個內部。

+0

你可以給我一個如何覆蓋這個方法的例子嗎? – sonoDamiano

+0

只需創建所需的UIView類的子類,然後實現該方法。你知道如何創建一個子類並重寫一個我假設的方法嗎? – Joride

+0

我很抱歉,但我從來沒有做過這樣的事情,我是Objective-C的初學者。 – sonoDamiano

0

每次用戶點擊表格行時,都會創建一個InvoiceDetailsVC對象,以便該視圖始終在該點不可見。您可能需要在self.splitviewController中存儲對當前所選行/索引路徑的引用,並檢查用戶是否正在竊聽相同的row/indexPath。 請注意,表數據源(即數組)必須是靜態的,或者您應該相應地更新對當前選擇的row/indexPath的引用。

例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSLog(@"did select row at index path %ld",(long)indexPath.row); 

    if ([self.splitViewController.currentlySelectedIndexPath compare:indexPath] != NSOrderedSame) { 

     InvoiceDetailsVC *detailsVC = [[InvoiceDetailsVC alloc]initWithFrame:CGRectMake(321, 0,708, 709)]; 
     [self.splitViewController.view addSubview:detailsVC]; 

     // Update reference to currently selected row/indexpath 
     self.splitViewController.currentlySelectedIndexPath = indexPath; 

    } else { 
     // The currently visible view is the same. Do nothing 
    } 
} 
0

使用窗口的的UIView屬性。 如果尚未添加,它會返回

以上應該工作,如果沒有 創建一個類屬性,並把它作爲一個標誌,檢查是否detailsVC已添加到屏幕或不。

例如, 在InvoiceDetailsVC.h頭文件或者.m文件,在viewDidLoad中添加以下代碼

@property BOOL addedToScreen; 

您的視圖 - 控制的

-(void)viewDidLoad{ 
//your code above 
self.addedToScreen =NO; 
} 

最後,在你的didselectRow

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if(self.addedToScreen== NO){ 
     InvoiceDetailsVC *detailsVC = [[InvoiceDetailsVC alloc]initWithFrame:CGRectMake(321, 0,708, 709)]; 
     [self.splitViewController.view addSubview:detailsVC]; 
     self.addedToScreen =YES; 
    } 
} 

希望這有助於,

相關問題