Here is How you Use a Class in Objective-C

In a previous post I talked about how to code a class in Objective-C. Of course, you need to actually instantiate a class to use it in your app. Here is how you would use that class in your app delegate.

AppDelegate.h

#import <UIKit/UIKit.h>
#import "MyClass.h"

@interface objectivecexamplesAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     MyClass *myObject = [[MyClass alloc] init];
     myObject.name = @"My Name";
     [myObject writeStateToLog];

     self.window.backgroundColor = [UIColor whiteColor];
     [self.window makeKeyAndVisible];
     return YES;

}

@end

7 Responses to Here is How you Use a Class in Objective-C

  1. snoonan January 23, 2009 at 6:37 pm #

    Excellent article! This is exactly the kind of stuff I need right now to wrap my head around the right away to do useful things in Objective-C.

    Nothing beats small examples like this, and then tying it back to some real iPhone applications is perfect. Thanks!

  2. Example of a Class in Objectiv January 23, 2009 at 8:37 pm #

    [...] for the first time.  I spent some time (much more than I had planned) to write a short tutorial on how to create a class in objective-c.  There does seem to be a lot of legwork needed to do something as simple as create a class for [...]

  3. colindean January 25, 2009 at 5:42 pm #

    For some reason, when I read the [[Address alloc] init] lines, I think I’m coding in Smalltalk.

  4. mattjdrake January 27, 2009 at 7:43 pm #

    @snoonan – Great, I found that the iPhone programming seemed disorienting at first – but it is funny how fast it can start to make sense once you have seen a few of the fundamentals demonstrated.

    @colindean – Nice, I do believe that Objective-C and Smalltalk are very closely related. It certainly uses similar conventions – or at least I’m told.

  5. iPhoneKicks.com February 4, 2009 at 5:41 pm #

    Here is How you Use a Class in Objective-C…

    You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

  6. 7 Essential Beginner iPhone Pr March 23, 2009 at 3:01 pm #

    [...] Here is How you Use a Class in Objective-C [...]

  7. Living Skull January 31, 2012 at 10:16 am #

    Hi all,

    I came across a very strange behavior recently. Having followed this tut, I tried the following code, class AddDeckSheetController defined and included:

    -(IBAction)deckTabHandler:(id) sender {

    switch ( [sender tag] ) {
    case 201: // Open Button
    break;
    case 202: // Plus Button
    AddDeckSheetController *addDeckSheet = [[AddDeckSheetController alloc] init];
    [addDeckSheet showAddDeckSheet:window];
    break;
    case 203: // Minus Button

    break;
    }
    }

    This did not work out, as my Xcode was expecting me to come up with splitting the declaration and creation of the var addDeckSheet. It would throw an error in the case 202, that it was expecting a statement (rather than a declaration).

    -(IBAction)deckTabHandler:(id) sender {

    AddDeckSheetController *addDeckSheet;

    switch ( [sender tag] ) {
    case 201: // Open Button
    break;
    case 202: // Plus Button
    addDeckSheet = [[AddDeckSheetController alloc] init];
    [addDeckSheet showAddDeckSheet:window];
    break;
    case 203: // Minus Button

    break;
    }
    }

    Cheers, Living

Leave a Reply

Affiliate Policy Disclosure

Switch to our mobile site