Few hints/tips I have found useful.

Discussion in 'iOS Development' started by lszanto, Jan 4, 2009.

  1. lszanto

    lszanto New Member

    Joined:
    Nov 8, 2008
    Messages:
    74
    Likes Received:
    0
    Device:
    2G iPod touch
    There have been a few things that I have found useful when starting applications. I'll post a few here and if you have any more feel free to post and i'll add them.

    • Rounding a float for display - In the string instead of %f simply place the number of decimal places you want e.g %.2f for 2 decimal places
    • Make a string from a number - NSString myString = [NSString stringWithFormat

      Please Register or Log in to view images

      "%d", number]
    • Set the text on a label - [labelName setText

      Please Register or Log in to view images

      "Hello, Label"]
    • Set alpha/transparency of a UI element - [UIElementName setAlpha:0.5] with 1.0 being 100% and 0.0 being 0

    Theres lots more that i'll add in time, please add yours aswell. Hopefully it saves you a bit of time searching round.
  2. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    Very nice. I can tell you that this will be very helpful to beginners who want to learn Objective-c. Keep them coming.
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You should be including math.h and doing your own rounding. Relying on merely printing a truncated (truncated, not rounded) float will create inconsistencies within your app. Especially if you need to actually work with that number again.
    roundf(float var);

    Look into math.h, there are several mega useful functions in there.


    Also, you should learn how to use properties. As in:
    label.text = "Some text.";
    object.alpha = 0.5;

    It will make life a lot easier for you. Especially when you start referencing your own stuff in other parts of your app. Instead of creating a whole bunch of functions similar to this:
    - (NSString *)getValueForAString {
    return aString;
    }
    and calling aDifferentString = [myObject getValueForAString]; in the referring object, you can simply set aString as a property of your object and refer to it. simplifting the above to
    aDifferentString = myObject.aString; //That's it.
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I was going to add the picture.alpha = 1.0; in there. Didn't know about the label one though, haven't used them much though. Thanks.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I was trying to figure out the label one as well I tried tons of things well the label one will help me anyway thanks. also

    Code:
    textView.text = @"This isn't really useful as text views are user inputted"
    
    textField.text = @"This can be useful sometimes"
    
    
    properties are really helpful IMO.

Share This Page