【问题标题】:iPhone: force from subview to remove itselfiPhone:从子视图强制删除自身
【发布时间】:2010-06-23 01:59:02
【问题描述】:

在 appdidfinishlaunch 中,我正在加载一个 tabbarcontroller 作为子视图,然后我加载另一个视图

MySubView * mySubView = [[MySubView alloc] init];
[window addSubview:mySubView];
[mySubView release];

我想在子视图中通过单击按钮来关闭顶层,所以我设置了一个 IBAction 并尝试了不同的方法来强制关闭实际视图:

// 1.
[self.view removeFromSuperview];

// 2.
id  *delegate = [[UIApplication sharedApplication] delegate];
[[[delegate view] objectAtIndex:0] removeFromSuperview];


//3.
[[[delegate window] view] removeFromSuperview];

那么我怎样才能从窗口弹出这个子视图呢?

干杯 西蒙

【问题讨论】:

    标签: iphone uiview uiwindow


    【解决方案1】:

    你可以做几件事。一种方法是为视图分配一个唯一标签,然后使用该标签获取它,所以:

    MySubView* mySubView = [[MySubView alloc] init];
    [mySubView setTag:100];
    [window addSubview:mySubView];
    [mySubView release];
    
    // later
    
    [[[delegate window] viewWithTag:100] removeFromSuperview];
    

    另一种方法是遍历窗口的子视图,直到找到一个是唯一类的实例,然后将其删除。所以:

    MySubView* mySubView = nil;
    for( UIView* view in [[delegate window] subviews] ) {
      if( [view isKindOfClass:[MySubView class]] ) {
        mySubView = (MySubView*)view;
        break;
      }
    }
    [mySubView removeFromSuperview];
    

    【讨论】:

    • 我得到了这个概念并尝试了两种变体,但我只得到错误。主要是:发送到实例的无法识别的选择器。我也改变了 [window addSubview:infostartView.view];
    • @Simon:您必须发布更多代码或更多地描述问题。如果您已按照上述方式设置,则不会出现任何错误。我不知道 infostartView 实例是什么,因为您没有提供任何此类上下文。
    猜你喜欢
    • 2012-04-06
    • 2011-10-19
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多