iOS: Getting Screen Size
To obtain the screen size based on the orientation and type of device, use this: screenWidth will return the width, and screenHeight, the height of your screen, even when your device is in the landscape orientation.
To obtain the screen size based on the orientation and type of device, use this: screenWidth will return the width, and screenHeight, the height of your screen, even when your device is in the landscape orientation.
You can store CGRect items into an NSArray. You need to store them as NSValue types: Here’s how to retrieve the items later on:
If you need to access the value of some variable in your app later on, be it in the same class or not, you might like to save it first using NSUserDefaults. Here’s an example with an NSString variable: If you have an NSInteger type variable instead, change your second line to: You may implement… Read More iOS: Save/Retrieve Data Using NSUserDefaults
When you have an NSArray object containing NSString objects in your View Controller, you might want to create a static version of it, especially when the user will only see the same values each time the view appears. First, add your C array pointers to NSString constants by following instructions in my earlier post. In… Read More iOS: Statically Create an NSArray Object in View Controller
Using NSNotificationCenter is a great way to send messages such that its corresponding receivers will be able to update their variables or interface values. First, create a name for your notification in your Constants file (go here if you don’t know how to create one), or anywhere appropriate so that your receiver(s) and sender will… Read More iOS: Using NSNotificationCenter to Send/Receive Messages
To round off a number in different ways:
Use the arc4random function to generate random numbers in a range. Suppose you have to generate a random number from 0 to 99. Do: int randomNum0to99 = arc4random() % 100; Or: int randomNum0to99 = arc4random_uniform(100); Between the 2 codes, the latter is preferred because: arc4random_uniform() will return a uniformly distributed random number less than upper_bound.… Read More iOS: Random Number Generation
This tip is especially useful because users might not notice the extra whitespace characters at the start and end of their text when they type into your text fields. The following code snippet will allow you to do that in iOS: NSString *newString = [someString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; You may use [NSCharacterSet whitespaceCharacterSet] if you do… Read More iOS: Remove Leading and Trailing Whitespaces in NSString Object
Sometimes you might like your status bar in your iOS app to be removed so that it’s more aesthetically pleasing. Try this snippet of code in your UIViewController classes:
It’s a good habit to keep your number and string constants in a file, instead of hardcoding them into your code. This minimises many inconveniences when you need to make changes to your code, since you only have to access that file to change those values. So how do you do that in Xcode 6… Read More iOS: Creating a File to Hold Constants