Question

i have to set some custom size parameters to a UIView according to the device orientation. i load the UIView which has NOT an own viewcontroller as follows:

in main.h

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

in main.m

@synthesize socialActionView;

. . .

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

if not needed anymore i remove it from the superview with

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

the log says (null) after removal

the size adjustments i do like this

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

works ok so far until i don´t remove the subview (socialAcitonView) from my main view. cause after that the self.socialActionView is not accessable anymore. i´ve seen a lot of examples here on stackoverflow like

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

or

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

or

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

}

or

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

but none of them works. the debugger always throws an exception cause of bad access. what (from my point of view) means the object does not exist any more. but how can the logger access it after i´ve removed it from the superview and say (null)?

i know it would have been better to give the view an dedicated view controller and put didRotateFromInterfaceOrientation there so i don´t have to check if the socialActionView is there. anyhow i´d like to ask if there is a way how i can check of the UIView is (null) nor not. before i change all my code to a view/viewcontroller couple.

any hints appreciated!

Was it helpful?

Solution

You are making it a retained property and then bypassing the retain attribute when you do this:

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

If you make that self.socialActionView = ... your property should hold its reference even after the view is removed from the subviews array.

(In fact, I'd recommend changing your synthesize statement to @synthesize socialActionView = _socialActionView; and putting a self.socialActionView wherever the compiler complains about that symbol.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top