Loops in Objective-C

In this very short article you will learn which types of loops are available in Objective-C and how they are working. Generally loops are very important when you want to do something again and again. Therefore we´re using loops. The three main loops are the „for“ loop, the „while“ loop and the „do while“ loop.

Continue reading

Fade in & Fade out Animation

In this short article I want to show you how to fade in or fade out an element – for example a label or a button. Generally I have written two subroutines – one for the fade in and the second one for the fade out animation. You may have noticed that the fade out subroutine is a little bit longer than the fade in animation. This is because we need to wait until the fade out animation is completed so that we can hide the label. Furthermore you can change the animation duration inside the “animateWithDuration” (seconds) method. The delay code gives you the opportunity to wait for example one second until the animation should begin. Please don´t forget to call the subroutine via “[self FadeInAnimation];” or “[self FadeOutAnimation];”.

Read more to view the example code!

Continue reading

Customizing the Navigation Bar Appearance

For developers it´s very important to give their developed application a special and awesome design. In this article I want to show you how you can customize the navigation bar inside your app. Generally there are many design elements which you can customize – here is what you´ll find in this article:

- add a background image or change the color of the Navigation Bar
- use an image as title instead of text
- change the design of the back button
- change the title font and size

Continue reading

The Power of NSLog

One of the most important function for an developer who develops in Objective-C is the „NSLog“ command. This function is able to show values inside the console window, which is included inside Xcode. Furthermore it´s a nice feature because you are able to control (check) some values without viewing them inside a label or a text field. Finally you can even view an integer, float or other formats inside the console with NSLog. For example you can directly enter your integer variable inside the parenthesis of the NSLog function. But when you want to display an text before or after the variable you can implement the following snippets without adding a new NSLog command to your code.

    //INT
    int YourINT = 235;
    NSLog(@"This is you INT value: %i", YourINT);
    // %i is used for a int type
    
    //FLOAT
    float YourFLOAT = 3543.21;
    NSLog(@"This is you FLOAT value: %f", YourFLOAT);
    // %f is used for a float type
    
    //STRING
    NSString *YourString = @"Hello There";
    NSLog(@"This is you STRING value: %@", YourString);
    // %@ is used to implement the string

Please write a comment if there are any questions — I will reply to it as fast as possible.

Objective-C: Recognize the display size

Since there is an 4 inch screen iPhone or iPod touch available some developers need just a piece of code which recognizes the display size of the device. In this case the snippet will check for an 3.5 inch or an 4 inch display. Finally you can put your code inside the if query for the specific size.

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            NSLog(@"3.5 inch display");
        }
        if(result.height == 568)
        {
            NSLog(@"4 inch display");
        }
    }

Please write a comment if there are any questions — I will reply to it as fast as possible.

Tutorial: Display the current time in a label

In this article I want to show you how to get your current time zone displayed in a label. You probably won’t use this often, because the device will normally show the time within the status bar. Okay so let´s start!
First of all we will write a subroutine, because you probably want to refresh the time when it has changed:

- (void)updateTimeLabel {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormat setDateFormat:@"hh:mm"];
    TimeLabel.text = [dateFormat stringFromDate:[NSDate date]];
    [self performSelector:@selector(updateTimeLabel) withObject:self afterDelay:1.0];
 }

Okay let´s get the things explained here. In the first line the NSDateFormatter has been initialized. In the second line we have set the time zone – in this case to the UTC time. Next we have configured which date format we would like to have – in this case the label will just show hours and minutes. When you want to add the seconds as well simply add “:ss”. The next line will just give the generated string to our label. Now the last line is very important – there we defined that the subroutine will be called every second.

When you run the project now nothing will be shown in the label because we have to tell the “viewDidLoad” that we want call our subroutine. So we simply have to add this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self updateTimeLabel];
}

Now the current time will be shown by the label.

Converting different Data Types in Objective-C

Sometimes you need to get an integer value out from a string. For example you want to build a calculator app which has an text field as input for the numbers. The text field will just return you the string value of the content inside – so you have to convert the string to an integer value to calculate something. When you have done the calculation you can even display it in your text field again by doing the integer to string conversion. Of course you can use other data types like float instead of the integer – but you have to change the %i (stands for an integer) to %f (stands for a float value).

INT to NSString:

NSString *String = [NSString stringWithFormat:@"%d", yourInteger];

NSString to INT:

long fromString = [String integerValue];
    // you can use int instead of long

Combining multiple strings:

NSString *exampleString;
    NSString *string1;
    NSString *string2;
    
    exampleString = [NSString stringWithFormat:@"%@%@", string1, string2];

Please feel free to drop a comment below.