PDA

View Full Version : Question about multiple Views




sdd
Jan 10, 2011, 04:45 PM
Hi Guys,

This might seem like a silly question. I have just created a multi view application, with the views being loaded as subviews on the mainViewController. Once they are changed using the UIToolbar the Superview is removed and then the request view opened.

Everything loads and runs fine however at the top of the view below the status bar I have about a 15 point gap before the subView Loads.

So if I change my mainviewcontroller to the same background colour as the the different views everything looks fine it is when the colour is different on the mainViewController you can still see the top 15points. So basically it is like there is 15 points margin at the top of each view.

All my views only have text on so nothing fancy and my mainViewController only has a toolbar on the bottom.

New to this so I am not sure if I have made myself clear please let me know if you need anymore info.

Cheers,


Steve



dejo
Jan 10, 2011, 04:49 PM
It's probably more like a 20px gap, the same size as the status bar. To which view are you adding the new views as subviews? Can we see the code where you do this?

sdd
Jan 10, 2011, 05:05 PM
I have my original MultipleViewsViewController.xib (obviously my .m and .h class also)...

I then created Three More Classes (also creating Three XIB files also).

FirstViewController, SecondViewController and ThirdViewController

I initialised them in the header .h of the MultipleViewsViewController.h file

#import <UIKit/UIKit.h>

@class FirstViewController;
@class SecondViewController;
@class ThirdViewController;


@interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;

}

@property (nonatomic, retain) FirstViewController *firstViewController;
@property (nonatomic, retain) SecondViewController *secondViewController;
@property (nonatomic, retain) ThirdViewController *thirdViewController;


-(IBAction) loadSecondView:(id)sender;
-(IBAction) loadThirdView:(id)sender;
-(IBAction) loadFirstView:(id)sender;

-(void) clearView;

@end

and then used them and implemented in the following way...

@synthesize firstViewController;
@synthesize secondViewController;
@synthesize thirdViewController;




-(IBAction) loadFirstView:(id)sender {
[self clearView];
[self.view insertSubview:firstViewController.view atIndex:0];
}

-(IBAction) loadSecondView:(id)sender {
[self clearView];
[self.view insertSubview:secondViewController.view atIndex:0];
}

-(IBAction) loadThirdView:(id)sender {
[self clearView];
[self.view insertSubview:thirdViewController.view atIndex:0];
}

-(void) clearView {
if (firstViewController.view.superview) {
[firstViewController.view removeFromSuperview];
} else if (secondViewController.view.superview) {
[secondViewController.view removeFromSuperview];
} else {
[thirdViewController.view removeFromSuperview];
}
}


Obviously I linked these using UIViewController and set the Three new files as the subClass in the original MultipleViewViewController.xib file within InterfaceBuilder and difined the outlets for these and the actions for the toolbar etc The views are loading fine and without issues it's just missing the top :(

Thanks for the quick reply,

Steve

dejo
Jan 10, 2011, 05:45 PM
Do the three XIBs have any Simulated User Interface Elements set?

sdd
Jan 10, 2011, 05:53 PM
Yes they do they have the toolbar simulated? Could this be my issue? i might be able to put my phone down now and go to sleep :-).

Cheers Steve

sdd
Jan 11, 2011, 02:48 AM
I have changed this on the FirstController file but still hasn't chnaged the effect of the screen?

Cheers Steve

dejo
Jan 11, 2011, 11:33 AM
Ah, yeah. You probably want to leave the status bar as specified (as well as adding a simulated toolbar element, where appropriate) for your XIBs.

I believe there is some interesting frame setting going on with your approach (as I found when examining the frame for the view before it is inserted), as seen in the following code:

-(IBAction) loadFirstView:(id)sender {
[self clearView];
CGRect frame = firstViewController.view.frame;
NSLog(@"(%.0f, %.0f, %.0f, %.0f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
[self.view insertSubview:firstViewController.view atIndex:0];
}


So I think you need to follow a variation of this recommendation from the View Controller Programming Guide (http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW25):
If you plan to add a view to a window using the addSubview: method, it is recommended that you explicitly set the frame of the view after loading it from a nib file. Although nib files store the most appropriate size for root views, setting the frame explicitly guarantees that your view is sized and positioned correctly within the window.
I'm pretty sure the same holds true for insertSubview:atIndex: as well.

sdd
Jan 11, 2011, 01:53 PM
Hi and thanks for your reply,

I am new to this and am a little confused as the example (above) I followed from a book, so I am not sure if I am being informed the incorrect methods.

If so do I need to re-approach the way I perform toolbar and multiple view applications?

i.e. would the structure of my code and way it is formatted be rejected from the app store?

I have the status bar on and I am simulating the toolbar on all Three extra views.

I look forward to your reply,

Steve

dejo
Jan 11, 2011, 02:15 PM
I am new to this and am a little confused as the example (above) I followed from a book, so I am not sure if I am being informed the incorrect methods.
Which book? And which example app? The more specific you can be about these kinds of things, the better. And perhaps things have changed in the SDK since that book was published. Have you checked to see if there is an errata for the book? Or a website for the book whether other readers are discussing similar issues?

If so do I need to re-approach the way I perform toolbar and multiple view applications?

i.e. would the structure of my code and way it is formatted be rejected from the app store?
If you feel comfortable with the approach from the book and are willing to invest the effort to tweak their code to work as you desire, then I see no need to re-approach it. But, if you feel wary of their approach, there are other ways to achieve the desired affect. (There's often more than one solution to any given problem of decent complexity when it comes to programming). Since I suspect you are still reading through the book, and not actually incorporating this into your own app, I would wait until all the fundamentals have been covered and then re-evaluate what your options are when it comes to making a real app.

Oh and it probably wouldn't be rejected from the App Store unless the UI quirkiness affects the usability of the app.

sdd
Jan 11, 2011, 03:20 PM
I am using the Sams Teach Yourself iphone in 24hours and working on hour 12. This is my first time using an object based coding language so I am just trying to get it right in my own head and don't like to move on if I can't rectify the problem. I have checked the errata and no mention to this topic.

I have downloaded the project off the website and looked at the completed app, which all views have a white background. If you change the main view bkcolor to green, the same thing happens on this.

Is there an easy fix to this? I.e. is your above example a fix to the method?

I am not sure if you know of this book and the methods used.

Steve

dejo
Jan 11, 2011, 04:06 PM
I am using the Sams Teach Yourself iphone in 24hours...
Thanks for that. Now we at least know one book that beginners should probably be wary of reading, since you are probably not the only person to have encountered this problem. :)

I have downloaded the project off the website and looked at the completed app, which all views have a white background. If you change the main view bkcolor to green, the same thing happens on this.
So, it looks like the authors of the book have missed this bug in their code. Perhaps there is a way you can contact them to let them know of this issue.

Is there an easy fix to this? I.e. is your above example a fix to the method?
Has the book covered setting frames for views yet? Here's some basic sample code to fix the issue but you may not understand it yet.

-(IBAction) loadFirstView:(id)sender {
[self clearView];
self.firstViewController.view.frame = CGRectMake(0, 0, 320, 416);
[self.view insertSubview:self.firstViewController.view atIndex:0];
}


The reason I use 416 for the height is because the visible frame of the view is 480 - 20 (height of statusbar) - 44 (height of toolbar). Of course, in a real app, you would want to avoid hardcoding values like this and instead get values from the view's environment instead. Just did it this way to quickly show you a simple approach.

P.S. Since you have defined synthesized accessors for your properties, I've taken the liberty to adjust the code to make sure you are using them. For this case, it doesn't make much difference but it's a good habit to get into. For further thoughts on this, see my other post (http://forums.macrumors.com/showpost.php?p=11512249&postcount=7) on naming ivars that back properties.

sdd
Jan 11, 2011, 04:36 PM
Thank you ever so much for this and I have not looked into frames yet but this will go a big way to helping me.

I have noted a few things in the book not mentioned in the errata and this ironically seems to be out of sync (page 300 corrections for example actually in a different place within the book).

I have looked on both website and they provide little/no place to provide feedback.

Thanks again, I shall continue as up to this point I was really enjoying and flowing nicely through it. I hope it hasn't knocked my confidence in the book now.

Steve

dejo
Jan 11, 2011, 04:45 PM
If you're comfortable with Objective-C, Beginning iPhone 3 Development: Exploring the iPhone SDK (http://www.amazon.com/Beginning-iPhone-Development-Exploring-SDK/dp/1430224592/ref=sr_1_1?s=books&ie=UTF8&qid=1294785734&sr=1-1) by David Mark and Jeff LaMarche is a very-well-received book (I recommend it also) and has a supporting website (http://www.iphonedevbook.com/) that is great as well.

sdd
Jan 15, 2011, 03:34 AM
I have just tried this and it works perfectly, thank you ever so much for this. I will be sure to pick this book up. I tried to look for books that other people have success with so I will be sure to add this to the bookshelf.

Cheers

Steve