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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-05-2012, 12:29 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 2
objlv is on a distinguished road
Default Is this a correct way to add/remove views?

Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created programmatically and all are in separate files:

In AppDelegate.m didFinishLaunchingWithOptions method I have these lines of code

Code:
self.rootViewController = [[rootViewController alloc]initWithNibName:@"rootViewController" bundle:nil];
self.window.rootViewController = self.rootViewController;
In rootViewController.m loadview method I have
Code:
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
self.firstView = [[FirstView alloc]init];
[self.view addSubview:self.firstView.view];
That code works fine - first view is displayed.

Let's continue

In FirstView.m switchViews method

NOTE: Please see the comments in code

Code:
self.secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];


// I think here secondView is added to rootViewController - right ?
[self.view.superview addSubview:self.secondView.view];

// Here first view is removed from rootViewController - right ?
[self.view removeFromSuperview];
Here is how I add/remove views.
Is this approach correct? Can you recommend a better solution?

I have read about UINavigationController, but I don't think it could be a solution in this case.

Last edited by objlv; 02-05-2012 at 12:32 PM.
objlv is offline   Reply With Quote
Old 02-05-2012, 12:39 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by objlv View Post
Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created programmatically and all are in separate files:

In AppDelegate.m didFinishLaunchingWithOptions method I have these lines of code

Code:
self.rootViewController = [[rootViewController alloc]initWithNibName:@"rootViewController" bundle:nil];
self.window.rootViewController = self.rootViewController;
In rootViewController.m loadview method I have
Code:
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
self.firstView = [[FirstView alloc]init];
[self.view addSubview:self.firstView.view];
That code works fine - first view is displayed.

Let's continue

In FirstView.m switchViews method

NOTE: Please see the comments in code

Code:
self.secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];


// I think here secondView is added to rootViewController - right ?
[self.view.superview addSubview:self.secondView.view];

// Here first view is removed from rootViewController - right ?
[self.view removeFromSuperview];
Here is how I add/remove views.
Is this approach correct? Can you recommend a better solution?

I have read about UINavigationController, but I don't think it could be a solution in this case.
No, it is not the right way to do it.

A view controller is supposed to manage the entire screen. You're not supposed to mix content from more than one view cotroller on the screen at a time. Doing so causes a variety of problems, including auto-rotation problems and memory problems.

If you're willing to limit yourself to iOS 5 and later, it is now supported. View controllers now let you register child view controllers who's content can be hosted by another view controller.

If you don't want to limit yourself to iOS 5 and later, you should refactor your code so a single view controller manages all the views. Then you could use normal view code to swap views in and out.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-05-2012, 12:48 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 2
objlv is on a distinguished road
Default

Thanks !
Аs far as I understand, you suggest me to create UIViewController and that controller to be set to window.rootViewController. Then that UIViewController should manage different UIView's, right ?
objlv is offline   Reply With Quote
Old 02-05-2012, 01:05 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by objlv View Post
Thanks !
Аs far as I understand, you suggest me to create UIViewController and that controller to be set to window.rootViewController. Then that UIViewController should manage different UIView's, right ?
Correct.

You don't have to limit yourself to a single view controller, just one at a time. You can still use a navigation controller or tab bar controller to manage multiple view controllers, or push a modal view controller, or various other options built into iOS.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-05-2012, 07:08 PM   #5 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 118
Feldspar is on a distinguished road
Default

You can also subclass UIView instead of UIViewController to retain some modularity, but some things still need to be handled by the controller.
__________________
Music Visualizer ~ Rabbit Hole http://bit.ly/mmlGOi
Feldspar is offline   Reply With Quote
Reply

Bookmarks

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: 402
18 members and 384 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, kukat, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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