{"id":517,"date":"2011-12-01T23:14:05","date_gmt":"2011-12-01T22:14:05","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=517"},"modified":"2014-11-01T14:57:45","modified_gmt":"2014-11-01T13:57:45","slug":"objective-c-basic-interface","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/objective-c-basic-interface\/","title":{"rendered":"Objective-C basic interface"},"content":{"rendered":"<p>In this tutorial, we will see how to create a basic interface in Objective-C with code only. It will be one TextField and one Button. Yeah, just that and we will need 6 files! And maybe we will use some inheritance.<\/p>\n<p>ActionScript 3 is really a smart language, it is very quick to create what we will do. Something like 10 lines of code&#8230; Anyway, in this tutorial I will not make comparisons between ActionScript 3 and Objective-C like <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/from-as3-to-objective-c\/\" target=\"_blank\">the previous one<\/a> unless it is really useful.<\/p>\n<p>Open Xcode and create a new iOS empty application, disable everything. Many files are created, but we will use only AppDelegate.h and AppDelegate.m In the AppDelegate we will init our applications.<\/p>\n<p><!--more--><\/p>\n<p>Objective-C is based on the MVC design pattern, so we need to implement it :<br \/>\nCreate a new UIViewController subclass file and name it MyViewController, don&#8217;t select XIB option, and create a folder &#8220;controllers&#8221;. Your files are created in the folder but you don&#8217;t see that in Xcode. Select the files, right click and select &#8220;New Group from Selection&#8221;. Rename it in &#8220;controllers&#8221;.<br \/>\nObjective-C doesn&#8217;t support package!<\/p>\n<p>Import your ViewController class in AppDelegate.h :<\/p>\n<pre lang=\"objc\" line=\"1\">#import <UIKit\/UIKit.h>\r\n#import \"MyViewController.h\"\r\n\r\n@interface AppDelegate : UIResponder <UIApplicationDelegate> {\r\n    \r\n    MyViewController *myViewController;\r\n}\r\n\r\n@property (nonatomic, retain) MyViewController *myViewController;\r\n\r\n@property (strong, nonatomic) UIWindow *window;\r\n\r\n@end\r\n<\/pre>\n<p>Then open its implementation file (.m) :<br \/>\nSynthesize the property :<\/p>\n<pre lang=\"objc\">@synthesize window = _window, myViewController;<\/pre>\n<p>Release the object in the dealloc method :<\/p>\n<pre lang=\"objc\">\r\n- (void)dealloc\r\n{\r\n    [_window release];\r\n    \r\n    [myViewController release];\r\n    \r\n    [super dealloc];\r\n}<\/pre>\n<p>Now we need to add our ViewController, go inside the <em>application:didFinishLaunchingWithOptions:<\/em> method and add two lines like that :<\/p>\n<pre lang=\"objc\" line=\"1\">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\r\n{\r\n    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];\r\n    \r\n    \/\/ Override point for customization after application launch.\r\n    \r\n    self.window.backgroundColor = [UIColor whiteColor];\r\n    \r\n    myViewController = [[MyViewController alloc] init];\r\n    \r\n    [_window addSubview:myViewController.view];\r\n    \r\n    [self.window makeKeyAndVisible];\r\n    return YES;\r\n}<\/pre>\n<p>Now we create an UIView class, I called it MyView. Create a folder &#8220;views&#8221; and &#8220;Group&#8221; your two new files. In the header file, we create 3 properties and a new constructor :<\/p>\n<pre lang=\"objc\" line=\"1\">#import <UIKit\/UIKit.h>\r\n\r\n@interface MyView : UIView {\r\n    \r\n    NSString *myText;\r\n    UIColor *colorText;\r\n    UIButton *myButton;\r\n}\r\n\r\n@property (nonatomic, retain) NSString *myText;\r\n@property (nonatomic, retain) UIColor *colorText;\r\n@property (nonatomic, retain) UIButton *myButton;\r\n\r\n- (id) initWithFrame:(CGRect)frame withText:(NSString *)text andColor:(UIColor *)color;\r\n\r\n@end<\/pre>\n<p>Then implement your file :<\/p>\n<pre lang=\"objc\">@synthesize myText, colorText, myButton;<\/pre>\n<p>In the new constructor we create our button, and we add the textfield in an other method.<\/p>\n<pre lang=\"objc\" line=\"1\">- (id) initWithFrame:(CGRect)frame withText:(NSString *)text andColor:(UIColor *)color {\r\n    \r\n    self = [super initWithFrame:frame];\r\n    if (self) {\r\n        \/\/ Initialization code\r\n        \r\n        myText = [[NSString alloc] initWithString:text];\r\n        \r\n        colorText = color;\r\n        \r\n        myButton = [UIButton buttonWithType:UIButtonTypeInfoLight];\r\n        [myButton setFrame:CGRectMake(10, 80, 30, 30)];\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n- (void) dealloc {\r\n    \r\n    [myText release];\r\n    [colorText release];\r\n    [myButton release];\r\n    \r\n    [super dealloc];\r\n}<\/pre>\n<p>And finally uncomment the <em>drawRect:<\/em> method which is called when a new object is created :<\/p>\n<pre lang=\"objc\" line=\"1\">- (void)drawRect:(CGRect)rect\r\n{\r\n    \/\/ the background becomes black, don't know why...\r\n    [colorText setFill];\r\n    [myText drawAtPoint:CGPointMake(100, 100) withFont:[UIFont systemFontOfSize:24]];\r\n}<\/pre>\n<p>Note that coordinates (0, 0) are top left on iOS app, but bottom left on mac!<\/p>\n<p>So at the moment we have just set up our view with some params. Now we add our view to the view controller :<\/p>\n<pre lang=\"objc\" line=\"1\">#import <UIKit\/UIKit.h>\r\n#import \"MyView.h\"\r\n\r\n@interface MyViewController : UIViewController {\r\n    \r\n    MyView *myView;\r\n}\r\n\r\n@property (nonatomic, retain) MyView *myView;\r\n\r\n- (void) wasTapped;\r\n\r\n@end<\/pre>\n<p>We have created a method <em>wasTapped<\/em> to inform the user that the button is pressed.<\/p>\n<pre lang=\"objc\">@synthesize myView;<\/pre>\n<p>And add these methods :<\/p>\n<pre lang=\"objc\" line=\"1\">- (void) loadView {\r\n    \r\n    CGRect rect = [UIScreen mainScreen].applicationFrame;\r\n    self.view = [[UIView alloc]initWithFrame:rect];\r\n    \r\n    myView = [[MyView alloc] initWithFrame:rect withText:@\"MyText\" andColor:[UIColor greenColor]];\r\n    \r\n    [self.view addSubview:myView];\r\n    \r\n    [self.view addSubview:myView.myButton];\r\n    [myView.myButton addTarget:self action:@selector(wasTapped) forControlEvents:UIControlEventTouchUpInside];\r\n}\r\n\r\n- (void) wasTapped {\r\n    \r\n    NSLog(@\"Button clicked\");\r\n}\r\n\r\n- (void) dealloc {\r\n    \r\n    [myView release];\r\n    \r\n    [super dealloc];\r\n}<\/pre>\n<p>Now compile, you should have already your green textfield and a button which send &#8220;Button Clicked&#8221; in the console!<\/p>\n<p>I think it&#8217;s time for some explanation :<br \/>\nIn the AppDelegate file, we can&#8217;t add a view directly, we need to add its view controller thanks to these methods :<\/p>\n<pre lang=\"objc\">myViewController = [[MyViewController alloc] init];\r\n[_window addSubview:myViewController.view];<\/pre>\n<p>Then we have added the view to our view controller thanks to the <em>loadView<\/em> method. It comes from UIViewController! Afterwards we have initialized our MyView class, giving some params, and finally added it and its button :<\/p>\n<pre lang=\"objc\" line=\"1\">myView = [[MyView alloc] initWithFrame:rect withText:@\"MyText\" andColor:[UIColor greenColor]];\r\n    \r\n[self.view addSubview:myView];\r\n[self.view addSubview:myView.myButton];<\/pre>\n<p>We didn&#8217;t need to add the textfield like we did with the button, because we use its method <em>drawAtPoint<\/em> which doesn&#8217;t exist on a UIButton.<\/p>\n<p>And the last line of <em>loadView<\/em> method, add an event to our button :<\/p>\n<pre lang=\"objc\">[myView.myButton addTarget:self action:@selector(wasTapped) forControlEvents:UIControlEventTouchUpInside];<\/pre>\n<p>The action parameter need to be explained : Xcode is waiting for a <em>SEL<\/em> object. A SEL is simply a pointer reference to a function, we already used something like that in AS3 with listeners :<\/p>\n<pre lang=\"actionscript3\">addEventListener(MouseEvent.CLICK, myFunction);\r\nfunction myFunction(mEvt:MouseEvent):void { trace('ok');}<\/pre>\n<p>In Objective-C we used the keyword <em>@selector(functionName)<\/em>.<\/p>\n<p>Now it&#8217;s time to create our first object. Create a new file, MyParentObject, which extend NSObject, and group it in objects folder.<\/p>\n<pre lang=\"objc\" line=\"1\">#import <Foundation\/Foundation.h>\r\n\r\n@interface MyParentObject : NSObject {\r\n    \r\n        NSString *myText;\r\n}\r\n\r\n@property (nonatomic, retain) NSString *myText;\r\n\r\n@end<\/pre>\n<pre lang=\"objc\" line=\"1\">#import \"MyParentObject.h\"\r\n\r\n@implementation MyParentObject\r\n\r\n@synthesize myText;\r\n\r\n- (id) init {\r\n    \r\n    self = [super init];\r\n    \r\n    if (self)  {\r\n        \r\n        myText = [[NSString alloc] initWithString:@\"My Parent Text\"];\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n- (void) dealloc {\r\n    \r\n    [myText release];\r\n    \r\n    [super dealloc];\r\n}\r\n\r\n@end<\/pre>\n<p>Then create a new NSObject class, MyObject which extends MyParentObject.<\/p>\n<pre lang=\"objc\" line=\"1\">#import <Foundation\/Foundation.h>\r\n#import \"MyParentObject.h\"\r\n\r\n@interface MyObject : MyParentObject {\r\n    \r\n    UIColor *myColor;\r\n}\r\n\r\n@property (nonatomic, retain) UIColor *myColor;\r\n\r\n@end<\/pre>\n<pre lang=\"objc\" line=\"1\">#import \"MyObject.h\"\r\n\r\n@implementation MyObject\r\n\r\n@synthesize myColor;\r\n\r\n- (id) init {\r\n    \r\n    self = [super init];\r\n    \r\n    if (self)  {\r\n        \r\n        myColor = [UIColor greenColor];\r\n        \r\n        myText = @\"My overrided text\";\r\n    }\r\n    \r\n    return self;\r\n}\r\n\r\n- (void) dealloc {\r\n    \r\n    [myColor release];\r\n    \r\n    [super dealloc];\r\n}\r\n\r\n@end<\/pre>\n<p>Then we just need to init MyObject in the ViewController and added its properties to the View Constructor :<\/p>\n<pre lang=\"objc\">myView = [[MyView alloc] initWithFrame:rect withText:myObject.myText andColor:myObject.myColor];<\/pre>\n<p>You&#8217;ve to think to synthesize it, init it, and release it!<\/p>\n<p>Then compile, that&#8217;s it!<\/p>\n<p><strong><a href=\"http:\/\/www.aymericlamboley.fr\/blog\/wp-content\/uploads\/2011\/12\/BasicInterface.zip\">MyZip<\/a><\/strong>.<\/p>\n<p>In this tutorial we saw the 3 primary objects (NSObject, UIView and UIViewController) and how to assemble them. If you are creating an app for the iPhone &#038; iPad, you will use Xcode&#8217;s Interface Builder with xib file. It is really smart, you just have to drag &#038; drop interfaces and &#8220;draw line&#8221; to make connection between them and code.<\/p>\n<p>On the internet, some people use NSRect NSColor objects for their view and design. I couldn&#8217;t use most of the NS object in my example&#8230; maybe there is a difference between an app on Mac and iOS. If someone could explain it, that would be great!<br \/>\nFinally it&#8217;s not learning a new language which is hard, it&#8217;s learning its API \ud83d\ude09<\/p>\n<p>I don&#8217;t think that I will made basic tutorials on creating iPhone apps, there are already lots of resources on the internet. I will focus on game engine, Cocos2D and Sparrow!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will see how to create a basic interface in Objective-C with code only. It will be one TextField and one Button. Yeah, just that and we will need 6 files! And maybe we will use some inheritance. ActionScript 3 is really a smart language, it is very quick to create what &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/objective-c-basic-interface\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Objective-C basic interface<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[98,5,97],"tags":[99,56,100],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/517"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":12,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":1284,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/517\/revisions\/1284"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}