Ios 检查是否已显示我的UIView的最佳方法是什么?

Ios 检查是否已显示我的UIView的最佳方法是什么?,ios,objective-c,ipad,uiview,didselectrowatindexpath,Ios,Objective C,Ipad,Uiview,Didselectrowatindexpath,检查屏幕上是否已显示我的UIView的最佳方法是什么。我有一个UISplitViewController,在其中我尝试在点击tableViewCell后在自定义UIView中显示详细信息。我想做的就是避免重复已经显示的视图,或者如果可能的话关闭当前视图并显示新视图 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"did select row a

检查屏幕上是否已显示我的UIView的最佳方法是什么。我有一个UISplitViewController,在其中我尝试在点击tableViewCell后在自定义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];
}
谢谢你的帮助
这是最好的方法

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

因为当视图出现在屏幕上时,
view.window
是唯一的值

另一个选项是覆盖willMoveToSuperview:保持此内部视图。

用户每次点击表行时,您都在创建一个
InvoiceDetailsVC
对象,因此视图在该点上始终不可见。您可能希望在
self.splitviewController
中存储对当前选定行/indepath的引用,并检查用户是否正在点击相同的
行/indepath
。 请注意,表数据源(即数组)必须是静态的,或者您应该相应地更新对当前选定的
行/indepath
的引用

例如:

- (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
    }
}

使用UIView的窗口属性。 如果尚未添加,它将返回nil

如果没有,上述方法应该有效 创建一个类属性并将其用作一个标志,以检查您的detailsVC是否已添加到屏幕中。

比如说,, 在InvoiceDetailsVC.h头文件或.m文件中,添加以下代码

@property BOOL addedToScreen;
在ViewDid中加载您的viewcontroller

-(void)viewDidLoad{
//your code above
self.addedToScreen =NO;
}
最后在你的那排

 - (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;
    }
 }

希望这有帮助,

它对我不起作用,因为我尝试加载UIView而不是UIViewController。InvoiceDetailsVC是一个自定义uiview。因此,此方法对于我的自定义uiview不可见。在我的故事板中,我添加了UISplitViewController作为UITabBarController的选项卡项,因为我无法与detailsViewController通信,因为splitviewcontroller必须是应用程序的根。我尝试将所选UITAbleviewCell的详细信息显示为splitviewcontroller的子视图。我的问题还有其他解决方案吗?你能给我一个如何重写此方法的示例吗?只需创建所需UIView类的子类,并实现该方法。你知道如何创建一个子类并重写我假设的方法吗?很抱歉,我从来没有做过这样的事情,我是objective-c的初学者。或者给我举个例子,如果这对你来说不是问题的话?在这种情况下,我想你应该退一步,多学一点面向对象编程。