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




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!
[...] 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 [...]
For some reason, when I read the [[Address alloc] init] lines, I think I’m coding in Smalltalk.
@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.
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…
[...] Here is How you Use a Class in Objective-C [...]
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