[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[coco8] iOS - Hello World Tutorial

Uploaded Image: alert.gifBefore starting this tutorial, be sure you have met all of the requirements for iOS with S8 development.
Specially the download of coco8 contribution.
This contribution is our starting point for developing iOS apps with S8.

Tutorial: Hello World

1) Open the Xcode coco8 project
What are the most important components You'll find in this project?
At this moment, pay attention to:
Uploaded Image: relevantProjectComponents.gif

If You haven't run the coco8 app in your iOS device before, this is a good time to do it.
(You'll discover a lot of interesting things exploring this app!)
NoteThe coco8 application is under continuos development; it is our source and guide to evolve to a robust dev. toolset for iOS; so it is reccomended to join our iOS testers group to download updates.

The First Screen We see when We run the coco8 app looks like:
Uploaded Image: coco8FirstScreen.png

It is a tabbed application. It's navigation is based on a Tab Bar (we insist here, it is completelly written in S8; so, you only need to know how to change the image contents to make your way to your own app).

THIS IS THE STARTING POINT FOR BUILDING YOUR S8 - iOS APP

2) Rename the project to your own project name
Uploaded Image: rename1.gif

Uploaded Image: rename2.gif


3) Our first goal will be to learn how to add new tabs associated with custom functionality, or remove the tabs we don't need or even remove the entire TabBar...

It's time to dive into Smalltalk


Why does this TabBar appear in our app?. Let's explore some of the app delegate methods:

! Coco8AppDelegate methodsFor: #ObjC !
applicationDidFinishLaunching: application
	" Private - Implements ObjC's post App launch action. "

	<objC: method>
	self window
		frame: UIScreen mainScreen bounds;
		rootViewController: self tabBarController;
		makeKeyAndVisible.
	NSFileManager installHooks.! !

It defines a TabBar as the rootViewController


! Coco8AppDelegate methodsFor: #tabs !
tabsSpecification
	" Private - Specification of main application tabs. "

	^#(
	#( #U8ApplicationsController #apps )
	#( #U8WorkspaceNavigationController #workspace )
	#( #U8BrowserNavigationController #browse )
	#( #U8MessagesNavigationController #messages )
	#( #U8TranscriptController #transcript )
	#( #U8InspectorNavigationController #inspector )
	#( #U8ServiceController #u8 )
	#( #U8SettingsController #settings )
	#( #U8ServersController #network )
	#( #PolygonViewController #polygon )
	) select: [:tuple| Smalltalk includesKey: tuple first ]! !


It defines the TabBar specification
Our first approach will be to add a new tab with our Hello World

Let's start with the User Interface
Xcode menu/File/New/File.../iOS/User Interface/Storyboard
Uploaded Image: storyboard0.jpg
Uploaded Image: storyboard1.jpg

4) Our app is just another Hello World Application: The User will type his or her name, and the application will print a personalized salutation. For example if the user types "Alex" the app will print "Good Morning Alex", or "Good Evening Alex"...and so on, depending on the current time
Uploaded Image: UIPaint.gif

5) iOS Applications are usually MVC based. We Just painted the View. That view will be associated with one Controller: The HelloWorldViewController
Uploaded Image: VC.jpg

Note: HelloWorldViewController class does not exist in the Xcode project. It Will be implemented in S8 Smalltalk!!!
                        <subviews>
                            <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" ... id="W96-dh-CLq">
                                <rect key="frame" x="16" y="86" width="288" height="30"/>
                                ...
                            </textField>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" ... id="mcm-yA-qCm">
                                <rect key="frame" x="16" y="289" width="288" height="127"/>
                                <fontDescription key="fontDescription" name="HelveticaNeue-Medium" family="Helvetica Neue" pointSize="20"/>
                                <color key="textColor" red="0.0" green="0.0" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill"  ... id="ziH-JK-PzL">
                                <rect key="frame" x="114" y="172" width="93" height="89"/>
                              ...
                              ...
                            </imageView>
                            ...
                            ...
                    </view>
                    
                    <connections>
                        <outlet property="dayMomentImageView" destination="ziH-JK-PzL" id="Abc-jq-542"/>
                        <outlet property="nameTextField" destination="W96-dh-CLq" id="PSC-aC-djj"/>
                        <outlet property="salutationLabel" destination="mcm-yA-qCm" id="Zqy-ai-6U1"/>
                    </connections>

Note that the destination field value of each outlet should be the same as the id value of the corresponding subview

6) HelloWorldViewController Smalltalk class
UIViewController
	subclass: #HelloWorldViewController
	instanceVariableNames: ''
	classVariableNames: ''
	category: #HelloSample!

! HelloWorldViewController class methodsFor: #ObjC !
implementation
	" Private - Return the implementation literal of the receiver. "

	^(#outlets -> #(nameTextField salutationLabel dayMomentImageView))
	,(#actions -> #(#sayHello:))
	,(#methods -> #(
		#viewDidLoad
		#(#textFieldShouldReturn: 'BOOL id')
		))! !

HelloWorldViewController buildTypedAccessors: #(
	#( salutationLabel #UILabel )
	#( nameTextField #UITextField )
	#( dayMomentImageView #UIImageView )
)! !


! HelloWorldViewController methodsFor: #ObjC !
viewDidLoad
	" Private - Load/Set the view. "


	<objC: method>
	self salutationLabel text: ''.
	self nameTextField delegate: self.
	self nameTextField
		addTarget: self
		action: #sayHello:
		forControlEvents: 262144 "UIControlEventEditingDidEnd".
	^self super: #{arguments}! !


! HelloWorldViewController methodsFor: #ObjC !
textFieldShouldReturn: aFieldHandle
	" Private - Load/Set the view. "

	<objC: method>
	^(UITextField @ aFieldHandle) resignFirstResponder! !


! HelloWorldViewController methodsFor: #ObjC !
sayHello: sender
	" Private - Refresh the table contents. "

	| userName dayMoment nowHours salutationText |
	<objC: method>
	userName := self nameTextField text.
	nowHours := DateTime now hours.
	(nowHours between:0 and:5)|| (nowHours >= 21) ifTrue:[ dayMoment := 'Night'].
	(nowHours between:6 and:11) ifTrue: [ dayMoment :='Morning'].
	(nowHours between:12 and:17) ifTrue: [ dayMoment :='Afternoon'].
	(nowHours between:18 and:20) ifTrue: [ dayMoment :='Evening'].
	self dayMomentImageView image: (UIImage named: dayMoment asLowercase).
	salutationText := 'Good ',dayMoment, String cr, userName.
	self salutationLabel text:salutationText.! !

! HelloWorldViewController class methodsFor: #loading !
storyboardName
	" Return the storyboard name of the receiver (or nil). "

	^#HelloWorld! !

7) Our app is almost done. Back to Coco8AppDelegate and add a new tab to the tabBar
! Coco8AppDelegate methodsFor: #tabs !
tabsSpecification
	" Private - Specification of main application tabs. "

	^#(
	#( #HelloWorldViewController #hello )
	#( #U8WorkspaceNavigationController #workspace )
	#( #U8BrowserNavigationController #browse )
	#( #U8MessagesNavigationController #messages )
	#( #U8TranscriptController #transcript )
	#( #U8InspectorNavigationController #inspector )
	#( #U8ServiceController #u8 )
	#( #U8SettingsController #settings )
	#( #U8ServersController #network )
	#( #PolygonViewController #polygon )
	) select: [:tuple| Smalltalk includesKey: tuple first ]! !

8)Edit build.js:
add emmit('HelloWorld.st'); to include our new stuff in the image

9) Add some icons to the project resources

10) Create the new Smalltalk image (Execute the build script - _build.sh - in Mac OSX Terminal)
11) If You have a developer iOS account You are ready to run Your app in your device!

Where to go from here?

If you just have finished your first app, You probably want to know how to publish it to the AppStore>[coco8] Publishing to the AppStore.

-