5

I'm sure this is some stupid mistake, but i'm trying for the past hour to remove a subview from my superview without any success.

On my first view i'm having

UIViewController *helpView = [[[UIViewController alloc] initWithNibName:@"HelpView" bundle:nil] autorelease];
[self.view addSubview:helpView.view];

And then inside helpView i have a button which is connected to an IBAction called "closeHelp" which just does the following:

- (IBAction) closeHelp{
    [self.view removeFromSuperview];
}

But this causes my app to crash with EXC_BAS_ACCESS for some weird reason, even those this is inside the HelpView, meaning self.view should be pointed to the correct subview..

Would appreciate your help

Thank you.
Shai.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
  • In what class do you have '-closeHelp'? Is it correctly wired up in interface builder? Do you have set the class in HelpView.xib to be that class? – Eiko Jul 09 '11 at 09:32

5 Answers5

5

As Andreas answered, you are trying to remove self.view from its super/parent view. You basically need to remove the helpView from its parent view.

so it should be

- (IBAction) closeHelp{
    [helpView removeFromSuperview];
}

But we dont know what is "helpView" in the above method. As we dont have any handle for it.

So our code should finally look like this.

#define HELP_VIEW_TAG 101 // Give tag of your choice

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];
[helpView release];

- (IBAction) closeHelp{
    UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
    [helpView removeFromSuperview];
}
  • Hey , thanks ! i tried doing this solution but now the subview won't even load ... (EXC_BAD_ACCESS) UIViewController *helpView = [[[UIViewController alloc] initWithNibName:@"HelpView" bundle:nil] autorelease]; helpView.view.tag = HELP_VIEW_TAG; [self.view addSubview:helpView.view]; [helpView release]; – Shai Mishali Jul 09 '11 at 08:43
  • Ok now it shows (did something stupid with the releases) , but the close button still won't work ... UIView *helpView = [self.view viewWithTag:101]; [helpView removeFromSuperview]; (did the tag number hard-coded just in case for the example) – Shai Mishali Jul 09 '11 at 08:47
  • I hope your help view is getting added as subview and is displayed. I am imagining that you have a close button on the help view and it is linked to the action method in your code above, which should dismiss itself, is this right? –  Jul 09 '11 at 08:50
  • Indeed, the helpView is being added as a subview and its displayed with [self.view addSubview:helpView.view]; Then i have the x button on the top right which calls the IBAction "closeHelp" (liked to it via interface builder on the touch up inside, plus i'm NSLog'ing so i know i'm getting inside the function) ... – Shai Mishali Jul 09 '11 at 08:54
  • Ok, my bad - there is probably something wrong with my connections since my button doesn't even output NSLOG , i'm trying to figure it out :) – Shai Mishali Jul 09 '11 at 08:57
  • Its really weird, even NSLog's i put inside my HelpView's "viewDidLoad" don't show up ... – Shai Mishali Jul 09 '11 at 09:02
  • please come to iphone/ipad room. –  Jul 09 '11 at 09:04
  • I love to use tags!..with less code[[self.view viewWithTag:HELP_VIEW_TAG] removeFromSuperview];..Cheers ;) – Mat Jul 09 '11 at 09:04
  • He is placing the IBAction method in the HelpViewController, which doesnt really exists. –  Jul 09 '11 at 09:06
  • Adding the subview with HelpView *helpView = [[[HelpView alloc] initWithNibName:@"HelpView" bundle:nil] autorelease]; Instead of UIViewController, fixed the viewDidLoad thing - it works now, but the button still doesn't hide the view - it just crashed like the beginning ... – Shai Mishali Jul 09 '11 at 09:12
  • @Shai Mishali, that is happening because the HelpViewController no more exists. This could be an immature answer, but just to see if it works, dont release or autorelease the helpView in your code, and see what happens. –  Jul 09 '11 at 09:15
  • Have you try to re-set your uibutton in IB?(like delete the action and re-set)?...this is a strange case if you have correctly connected your outlets. – Mat Jul 09 '11 at 09:18
  • @pratikshabhisikar You were right!! Now it completely works, but - When should i release the object in order to avoid a memory leak? ... – Shai Mishali Jul 09 '11 at 09:19
  • @Shai Mishali, you have enough reputation. Come to the room to have a discussion on this. Extending the comment thread is something that has to be avoided here. –  Jul 09 '11 at 09:26
  • I have enough and it says i have enough ("Congrats, you've gained the privilege – talk in chat and 5 other privileges learn more") , but when i come into chat it still just says "you need 20" even thought i have it... weird. – Shai Mishali Jul 09 '11 at 09:30
  • I suggest you to add the environment variable NSZombieEnabled(in Project->"Edit Active Executable") and set to YES to track which object launch the problem. – Mat Jul 09 '11 at 09:32
  • Currently this works, the only problem is i'm not sure where to release the object - I could do it on the dealloc of the homeview but it would be better if i could do it on the HelpView dealloc itself... Wish i could just go into chat but there is some bug with it for me... – Shai Mishali Jul 09 '11 at 09:54
  • @Shai Mishali, I think helpView need not be a controller. If you agree and if thats possible in your application, then use helpView as a view, not controller. You can create your helpView in a xib, and then load the xib using [NSBundle mainBundle] loadNibNamed:@"HelpView" owner: nil options:nil]; This method will return you an NSArray, which will have the HelpView loaded into it at index 0. Learn more about this, and if this is not possible, keep the controller as an instance variable. –  Jul 09 '11 at 10:02
  • And yet no one has realized that the helpView is being initizalied as a uiviewcontroller, which effectively removes all access to helpView code, which the button references. That's what's killing it. – Greg Jul 09 '11 at 12:46
2

The self.view does not point to your subview but the root view which your uiviewcontroller manages. You should probably remove only the last object in the subview stack, not the whole view, because now you are removing the whole help view.

Anyway, why do you not present the viewcontroller modally instead of doing this?

[self presentModalViewController:helpView animated:NO/YES];

helpView. modalTransitionStyle = //One of the constants below

UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl

Usually I am writing self.modalTransitionStyle = // One of the constants in the viewcontroller which will be presented modally, instead of spreading the code.

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • This method belongs into the HelpViewController anyway, and the last sentence has no meaning here. If you want to modify the view hierarchy, your view needs to be loaded anyway, and self.view is the appropriate way to reference the view. – Eiko Jul 09 '11 at 09:39
1

You are initializing helpView as a UIViewController.
Make sure you have #import "HelpView.h" (or whatever the helpView .h file is called) in the .h file of the view controller where you are initializing it.

Then, use this code:

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
[self.view addSubview:helpView.view];

That should fix it.

Greg
  • 9,068
  • 6
  • 49
  • 91
  • He is basically, in the code, trying to remove self.view from its superview, which probably should be nil. –  Jul 09 '11 at 08:27
0

The easiest solution for me eventually was to just define my XIB's file owner as the same class as the parent controller, meaning the parent controller would control both the parent and the subview, which just makes a lot easier. :)

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
-1
Declare the help view on calss level.

in.h file 

@class HelpView;
..
@interface
{
HelpView *helpView;
}
@property(nonatomic,retain)HelpView*  helpView;


In.m file 
#import "HelpView"
@synthensize helpView;



now add this Code where you want 

helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];


- (IBAction) closeHelp{
    //UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
    [helpView removeFromSuperview];
}

-(void)dealloc { [helpView release]; }

dark
  • 447
  • 1
  • 9
  • 21