Define constant based on device height

Several times we need to define constants based on device height. For example, Wallpaper applications we need to show different image based on device height.

Defining constants is pretty easy. But defining conditional data based constant is little tricky because It's impossible to get device type or height during preprocessing step. It is determined dynamically during runtime.

Another tricky point occurs while comparing floating values.

For my application i need to define different category id for iPhone 4 and iPhone 5.
The iPhone 5's screen height is 568.  Using [[UIScreen mainScreen] bounds].size.height gives us the height of a device.

So, we can use a macro
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568)

But problem is that [[UIScreen mainScreen] bounds].size.height returns float value. Several times based on values it can give you weird result. Here is a nice explanation how dangerous is it to compare floating point values.

 Although all of us are not computer scientists, can have a deep look at what every computer scientist should know about Floating-Point Arithmetic.

So, better if we use
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

You can get a nice detailed explanation regarding this at Macmade's answer at StackOverFlow

In my case, 

#define HOME_CATEGORY_ID_IPHONE_5 3
#define HOME_CATEGORY_ID_IPHONE_4 2

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )


#define HOME_CATEGORY_ID (IS_IPHONE_5 ? HOME_CATEGORY_ID_IPHONE_5 : HOME_CATEGORY_ID_IPHONE_4)

Cool Basics : IBOutlet and IBAction





Introduction

While developing iPhone applications,  one of the basic concept is IBOutlet and IBAction related to interface builder.

Basics

IBOutlet stands for Interface Builder Outlet.

An Outlet is a link from code to UI. If you want to show or hide an UI element, if you want to get the text of a textfield or enable or disable an element (or a hundred other things) you have to define an outlet of that object in the sources and link that outlet through the “interface object” to the UI element. After that you can use the outlet just like any other variable in your coding.

IBAction stands for Interface Builder Action.

IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them.

In other words, we need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.

viewcontroller.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIButton *btnHelloWorld;
}

- (IBAction)pressedOnHelloWorld:(id)sender;

@end


viewcontroller.m

- (IBAction)pressedOnHelloWorld:(id)sender {
    
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"This is my first iPhone Application"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
}