Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2010, 08:36 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default How do I remove a sub view from the view?

I use the following code to add subviews and release them when the user taps a button:

Code:
- (IBAction)mainMenuTouched {
	MainMenu *mainView = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
	[self.view addSubview:mainView.view];
	[Game release];
}
This code will take you from the game to the Main Menu. The only thing is, when I click the button, you can still hear the game running in the background. It's almost like I'm able to play the game from the main menu without seeing it. It's almost as if it's hidden instead of not there. I have been reading about this and I thought I was able to use [myView removeFromSuperview]; after [Game release]; but when I did that I got the warning: 'Game' may not respond to '+removeFromSuperview' (Messages without a matching method signature will be assumed to return 'id'
and accept '...' as arguments.) However, since this is just a warning, the app will still compile. So I go to the game and click the main menu button, and my app completely freezes up. Sorry if this sounds like a noob question. Go easy on me. I'm only 13. :)

Thanks for all your help in advance. And oh yeah. Did I mention programming is AWESOME? Thanks again.
Whitehk is offline   Reply With Quote
Old 06-09-2010, 10:19 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 107
JDave is on a distinguished road
Default

Code:
- (IBAction)mainMenuTouched {
	MainMenu *mainView = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view addSubview:mainView.view];
//[Game release];
}
Within the scope of the code you gave, releasing Game does not make sense... I suggest removing this statement, as it is likely a global variable and should be set to nil and released in viewDidUnload: and dealloc: . The way you have implemented it, [Game release] is not doing anything, per se. Maybe you meant [mainView release]; ?

If the code snippet you gave it indeed responsible for going from the Game screen to the Main Menu, then you have not actually stopped the Game viewController, and are actually stacking another view on top of it, via [self.view addSubview:mainView.view]. To prove this to you... add [self.view removFromSuperview:mainView.view]; and you will go back to the game...

like so:

Code:
- (IBAction)mainMenuTouched {
	MainMenu *mainView = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view addSubview:mainView.view];
[self.view removFromSuperview:mainView.view]; // THIS LINE 'UNDOES' THE PREVIOUS LINE'S CODE....
//[Game release];
[mainView release]; // PERHAPS THIS IS WHAT YOU MEANT TO SAY INSTEAD OF [Game release];
}
I suggest you create a 'dummy' "Utility Application" in XCode and see how it handles the transition between 2 view controllers (which is via lazy loading...), as this is how you can have 2 view controllers in memory at the same time. However, you will likely need to implement more code to go from 1 view controller to another, such as transferring variables (scores? level? time?) to the next view (this can be done in multiple ways... via code, an intermediate file (think plist) or other data storage method, stopping the game and music, and removing the view at the end.

The warning you are getting is not occurring in the code you provided, but may be related to how you are removing your 'view'... post the code where the Warning occurs, but I am guessing it is one of those warnings you can ignore if you are doing it correctly...

Good Luck.

Last edited by JDave; 06-09-2010 at 10:24 PM.
JDave is offline   Reply With Quote
Old 06-09-2010, 10:25 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by JDave View Post
Code:
- (IBAction)mainMenuTouched {
	MainMenu *mainView = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view addSubview:mainView.view];
//[Game release];
}
Within the scope of the code you gave, releasing Game does not make sense... I suggest removing this statement, as it is likely a global variable and should be set to nil and released in viewDidUnload: and dealloc: . The way you have implemented it, [Game release] is not doing anything, per se.

If the code snippet you gave it indeed responsible for going from the Game screen to the Main Menu, then you have not actually stopped the Game viewController, and are actually stacking another view on top of it, via [self.view addSubview:mainView.view]. To prove this to you... add [self.view removFromSuperview:mainView.view]; and you will go back to the game...

like so:

Code:
- (IBAction)mainMenuTouched {
	MainMenu *mainView = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:[NSBundle mainBundle]];
[self.view addSubview:mainView.view];
[self.view removFromSuperview:mainView.view]; // THIS LINE 'UNDOES' THE PREVIOUS LINE'S CODE....
//[Game release];
[mainView release]; // PERHAPS THIS IS WHAT YOU MEANT TO SAY INSTEAD OF [Game release];
}
I suggest you create a 'dummy' "Utility Application" in XCode and see how it handles the transition between 2 view controllers (which is via lazy loading...), as this is how you can have 2 view controllers in memory at the same time. However, you will likely need to implement more code to go from 1 view controller to another, such as transferring variables (scores? level? time?) to the next view (this can be done in multiple ways... via code, an intermediate file (think plist) or other data storage method, stopping the game and music, and removing the view at the end.

Good Luck.
Thanks for the quick reply. I really appreciate it. This is the code I used to remove the view:
[Game removeFromSuperView];
Thinking about it now, I don't know what I was thinking. It should be
[self.view removeFromSuperView:gameView.view]
right?

Last edited by Whitehk; 06-09-2010 at 10:36 PM.
Whitehk is offline   Reply With Quote
Old 06-11-2010, 02:29 PM   #4 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 107
JDave is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
Thinking about it now, I don't know what I was thinking. It should be
[self.view removeFromSuperView:gameView.view]
right?
The above code you noted is 'more' correct, but both methods you mentioned could potentially work.
JDave is offline   Reply With Quote
Old 06-11-2010, 02:48 PM   #5 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
Thanks for the quick reply. I really appreciate it. This is the code I used to remove the view:
[Game removeFromSuperView];
Thinking about it now, I don't know what I was thinking. It should be
[self.view removeFromSuperView:gameView.view]
right?
well, if your using [game removeFromSuperview] after [game release] it wont work. When you release game, you cant call it again. So instead, put the remving code before the releasing code
justill45 is offline   Reply With Quote
Reply

Bookmarks

Tags
remove subview, removefromsuperview, switching views, view, viewcontrollers

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 406
11 members and 395 guests
7twenty7, ChrisYates, djohnson, Duncan C, gmarro, hussain1982, Kirkout, Retouchable, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:05 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0