Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 6    Views: 62

Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
07/06/14 03:55 PM (10 years ago)

Question on where to put code

So I have my code that I use throughout my app all over the place, like this: //==================== //DETERMINE SCREEN SIZE //==================== if iPhone5 { // do this } else { //do this } Works great. I also use it when detecting ipad. My problem is, I need a different screen size for my home screen in my app, and I'm not sure where to put my code this time. For the ipad, it's easy, I just created a new XIB and named it filename~iPad and xcode knows what to do with it. Is there an equivalent for telling xcode that if a 3.5" screen is detected, to load a different xib? As it is right now, xcode just sees "iphone" and sets my home screen to my 4" XIB layout, so the bottom is gonna get chopped off for the iPhone 4 users. If I put my usual detection code in a viewdidload or something, it would be too late by then by the time the code fires. Thanks!
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
07/06/14 05:55 PM (10 years ago)
Override the init method by having it call the initWithNibName method for the super call, passing the name of the xib file you need to use based on the logic you are using.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
07/06/14 06:13 PM (10 years ago)
While nodding my head with what Chris correctly says, I add this: ////////////////////////////////////////////////////////////////// // initWithNibName ////////////////////////////////////////////////////////////////// - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil; { yourApp_appDelegate *appDelegate = (yourApp_appDelegate *)[[UIApplication sharedApplication] delegate]; if (appDelegate.rootDevice.isIPad) { self = [super initWithNibName:@"yourXib~iPad" bundle:nil]; } else { if ([UIScreen mainScreen].bounds.size.height == 568) { self = [super initWithNibName:@"yourXib_i5" bundle:nil]; } else { self = [super initWithNibName:@"yourXib_i4" bundle:nil]; } } return self; } Of course your xib/delegate names will be different. Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/07/14 05:12 AM (10 years ago)
Thanks smug and Chris. So smug, in your example, I'd just add that to the home screen plugins .m file? I think that's my biggest confusion, I don't know where to put it.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
07/07/14 05:36 AM (10 years ago)
Oh... yep. "If" your plugin already has an init section, comment it out and put this there. If your plugin 'does not' have an init section, paste this somewhere... I usually stick it up top, above or around 'viewWillAppear' and/or 'viewDidLoad'... 'Exactly' where is not as important as making sure it's 'outside' of any other method... Cheers! -- Smug
 
Angry Ninja
Aspiring developer
Profile
Posts: 1045
Reg: Aug 25, 2013
Maine
17,150
like
07/07/14 05:52 AM (10 years ago)
Worked perfectly :) Thanks for the direction guys
 
NCbuzz
Code is Art
Profile
Posts: 575
Reg: Sep 11, 2013
Lillington, NC
11,100
like
08/09/14 10:28 PM (10 years ago)
And then in a few months, get ready for... yourXib_i6 ;)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.