【问题标题】:What is the best way to check if my UIView has already been shown?检查我的 UIView 是否已显示的最佳方法是什么?
【发布时间】:2014-01-05 04:19:49
【问题描述】:

检查我的 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];
}

感谢您的帮助 问候

【问题讨论】:

    标签: ios objective-c ipad uiview didselectrowatindexpath


    【解决方案1】:

    使用 UIViewwindow 属性。 如果尚未添加,则返回 nil

    以上应该可以,如果不是 创建一个类属性并将其用作标志来检查您的 detailsVC 是否已添加到屏幕。

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

    @property BOOL addedToScreen;
    

    在视图控制器的 viewDidLoad 中

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

    希望这会有所帮助,

    【讨论】:

      【解决方案2】:

      每次用户点击表格行时,您都会创建一个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
          }
      }
      

      【讨论】:

        【解决方案3】:

        另一个选项是覆盖 willMoveToSuperview: 以保持内部。

        【讨论】:

        • 你能给我一个如何覆盖这个方法的例子吗?
        • 只需创建您想要的 UIView 类的子类,并实现该方法。你知道如何创建子类并覆盖我假设的方法吗?
        • 对不起,我从来没有做过这样的事情,我是objective-c的初学者。
        • 如果对您没有问题,或者给我看一个例子?
        • 那么在这种情况下,我认为你应该退后一步,多了解一些面向对象编程的知识。
        【解决方案4】:

        这是最好的方法..

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

        因为view.window 仅在视图出现在屏幕上时才具有价值。

        【讨论】:

        • 它对我不起作用,因为我尝试加载 UIView 而不是 UIViewController。 InvoiceDetailsVC 是一个自定义的 uiview。所以这个方法对于我的自定义 uiview 是不可见的。在我的故事板中,我将 UISplitViewController 添加为 UITabBarController 的 tabBarItem ,因为我无法与 detailsViewController 通信,因为 splitviewcontroleller 必须位于应用程序的根目录。我尝试将所选 UITableviewCell 的详细信息显示为 splitviewcontroller 的子视图。我的问题还有其他解决方案吗?
        猜你喜欢
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        • 2020-12-08
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 2011-05-25
        • 2012-08-29
        相关资源
        最近更新 更多