【问题标题】:how to check if if a UIView is still there?如何检查 UIView 是否仍然存在?
【发布时间】:2012-06-26 21:28:54
【问题描述】:

我必须根据设备方向为 UIView 设置一些自定义尺寸参数。我加载 没有自己的视图控制器的 UIView,如下所示:

在 main.h 中

@property (nonatomic, retain) IBOutlet UIView *socialActionView;

在 main.m 中

@synthesize socialActionView;

。 . .

socialActionView = [[[NSBundle mainBundle] loadNibNamed:@"SocialActionViewController" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:socialActionView];

如果不再需要,我将其从超级视图中删除

[self.socialActionView removeFromSuperview];
NSLog(@"%@",self.socialActionView.superview);

删除后日志显示 (null)

我喜欢这样的尺寸调整

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{
    if ((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
        if (self.socialActionView != NULL) {
            [self.socialActionView setBounds:CGRectMake(0, 0, 320, 480)];
            [self.socialActionView setCenter:CGPointMake(160, 240)];
            [self.scroll1 setContentSize:CGSizeMake(320, 600)];
            [self.scroll1 setBounds:CGRectMake(0, 0, 320, 428)];
            [self.scroll1 setCenter:CGPointMake(160, 266)];
        }
    } else if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
        if (self.socialActionView != NULL) {
            [self.socialActionView setBounds:CGRectMake(0, 0, 480, 320)];
            [self.socialActionView setCenter:CGPointMake(240, 160)];
            [self.scroll1 setContentSize:CGSizeMake(480, 600)];
            [self.scroll1 setBounds:CGRectMake(0, 0, 480, 268)];
            [self.scroll1 setCenter:CGPointMake(240, 186)];
        }
    }
} 

到目前为止工作正常,直到我不从我的主视图中删除子视图 (socialAcitonView)。导致之后 self.socialActionView 不再可访问。我在stackoverflow上看到了很多例子,比如

-(IBAction)showPopup:(id)sender {
    if(![[self myView] isDescendantOfView:[self view]]) { 
        [self.view addSubview:[self myView]];
    } else {
        [[self myView] removeFromSuperview];
}

-(IBAction)showPopup:(id)sender
{
    if (!myView.superview)
        [self.view addSubview:myView];
    else
        [myView removeFromSuperview];
}

if (!([rootView subviews] containsObject:[self popoverView])) { 
    [rootView addSubview:[self popoverView]];
} else {
    [[self popoverView] removeFromSuperview];

}

-(IBAction)showPopup:(id)sender {
    if([[self myView] superview] == self.view) { 
        [[self myView] removeFromSuperview];           
    } else {
        [self.view addSubview:[self myView]];         
    }
}

但它们都不起作用。调试器总是抛出异常导致错误访问。什么(从我的角度来看)意味着该对象不再存在。但是在我从超级视图中删除它并说(null)之后,记录器如何访问它?

我知道最好给视图一个专用的视图控制器并将 didRotateFromInterfaceOrientation 放在那里,这样我就不必检查 socialActionView 是否在那里。无论如何,我想问一下是否有办法检查 UIView 是否为(null)。在我将所有代码更改为视图/视图控制器对之前。

感谢任何提示!

【问题讨论】:

    标签: ios uiview reference subview superview


    【解决方案1】:

    您将其设为保留属性,然后在执行此操作时绕过保留属性:

    socialActionView = [[[NSBundle mainBundle] loadNibNamed:@"SocialActionViewController" owner:self options:nil] objectAtIndex:0];
    

    如果你设置self.socialActionView = ...,即使视图从子视图数组中删除,你的属性也应该保持它的引用。

    (事实上,我建议将您的 synthesize 语句更改为 @synthesize socialActionView = _socialActionView; 并在编译器抱怨该符号的任何地方放置 self.socialActionView。)

    【讨论】:

    • 太棒了!没错!多谢!只是为了理解:我在那里做了什么?启动了一个本地的socialActionView 实例?如果不将它投射到我定义它的块中,这可能吗?
    • 当您使用self. 时,您实际上是在经历@synthesize 创建的不可见方法,而不仅仅是更新实例变量。这些方法的行为取决于您在@property 语句中设置的属性。因此,在这种情况下,通过这些方法尊重您的 retain;直接进入变量不会。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2011-11-05
    • 1970-01-01
    • 2020-01-27
    • 2011-09-20
    • 2014-05-02
    • 2012-11-30
    相关资源
    最近更新 更多