iOS App Development with Swift.

Scope:

  1. Two screen
  2. Application Title:
  3. Hello World Text

Screenshots:

        
As you can see in screen 1 has one Label, one TextField, one Button, one scrollview, one image view, one segmented controller, one date picker and one button to go to next screen. Two more fields are hidden and you can see them once you scroll the scrollview and those components are slider and a simple view. The second view is simple and has one label, navigation title, back button, one table view and each cell of tableview contains one image and a text information for that cell. If you tap on any of the cell it will show you an alert. I kept this example pretty basic and I thought it would be fun learning things step by step. So lets now discuss on the code and see what we can learn from this simple tutorial.

Swift Project Creation Steps:

1. Launch Xcode (Current Version is Xcode_6_beta_7.dmg as on Sep 06, 2014).
2. Choose new project from launch window or File -> New Project.
or
3. Select template for your project. Choose 'Single View Application' for this example.
4. Select language as Swift (Objective C would be default option, please check and change it to Swift) and give some name to your project.
5. Next would be selecting the project folder location and done. So you are now ready with your first swift project template. Now just go ahead and download the source code from github url given below.

Source Code:

Please get the sample source code from GitHub Repository at  https://github.com/pawan4444/SwiftExample

Some Code for basic UI components:

//Adding a label
let helloWorldLabel = UILabel(frame: CGRect(x: 20, y: 60, width: 280, height: 30))
helloWorldLabel.text = "Hello World Example Application."
helloWorldLabel.font = UIFont.boldSystemFontOfSize(17.0)
helloWorldLabel.textAlignment = .Center
self.view.addSubview(helloWorldLabel)
//Adding a textField
let helloWorldTxtFld = UITextField(frame: CGRect(x: 20.0, y: 92.0, width: 280.0, height: 33.0))
helloWorldTxtFld.delegate = self
helloWorldTxtFld.borderStyle = .RoundedRect
helloWorldTxtFld.placeholder = "Enter your name please"
helloWorldTxtFld.textColor = UIColor.greenColor()
helloWorldTxtFld.textAlignment = .Center
self.view.addSubview(helloWorldTxtFld)
//Adding a button
let hiButton:UIButton = UIButton(type: .Custom) as UIButton
hiButton.frame = CGRect(x: 20, y: 140, width: 280, height: 34)
hiButton.backgroundColor = UIColor(red: 0.0, green: 0.423, blue: 0.87, alpha: 1.0)
hiButton.setTitle("Touch me, I am Button", forState: .Normal)
hiButton.setTitleColor(UIColor.redColor(), forState: .Normal)
hiButton.addTarget(self, action: "hiButton_Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(hiButton)
//Adding a scrollView
let scrollview: UIScrollView = UIScrollView(frame: CGRect(x: 20.0, y: 195.0, width: 280.0, height: 300.0))
scrollview.backgroundColor = UIColor.lightGrayColor()
scrollview.contentSize = CGSize(width: 280.0, height: 600)
self.view.addSubview(scrollview)
//Adding a imageView to scrollView
let myImageView = UIImageView(image: UIImage(named: "won.png"))
myImageView.center = CGPoint(x: scrollview.frame.width/2, y: myImageView.center.y)
scrollview.addSubview(myImageView)
//Adding a UISegmentedController to scrollView
let segmentController: UISegmentedControl = UISegmentedControl(items: ["Won","Lost"])
segmentController.selectedSegmentIndex = 0
segmentController.addTarget(self, action: "segmentController_Action:", forControlEvents: .ValueChanged)
segmentController.center = CGPoint(x: myImageView.center.x, y: 180)
scrollview.addSubview(segmentController)
//Adding a date picker to scrollView
//Bugs with UIDatePicker at Apple library
// 1. Datepicker code works well for deployment target 8.1 else causing segmentation fault 11
let datePicker = UIDatePicker(frame: CGRect(x: -20.0, y: 200.0, width: 0, height: 0))
datePicker.datePickerMode = .Date
datePicker.addTarget(self, action: "datePicker_Action:", forControlEvents: .ValueChanged)
datePicker.layer.borderWidth = 1.0
datePicker.layer.borderColor = UIColor.redColor().CGColor
scrollview.addSubview(datePicker)
//Adding a slider
let slider: UISlider = UISlider(frame: CGRect(x: 20.0, y:450.0 , width: 240.0, height: 20.0))
slider.minimumValue = 0.0
slider.maximumValue = 1.0
slider.value = 1.0
slider.addTarget(self, action: "slider_Action:", forControlEvents: .ValueChanged)
scrollview.addSubview(slider)
//Adding a view
myView = UIView(frame: CGRect(x: 20.0, y:480.0 , width: 240.0, height: 40.0))
myView.backgroundColor = UIColor.redColor()
scrollview.addSubview(myView)
//Adding a button
let nextButton:UIButton = UIButton(type: .Custom) as UIButton
nextButton.frame = CGRect(x: 20, y: 510, width: 280, height: 34)
nextButton.backgroundColor = UIColor(red: 1.0, green: 0.423, blue: 0.17, alpha: 1.0)
nextButton.setTitle("Let's Go to Next Page", forState: .Normal)
nextButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
nextButton.addTarget(self, action: "nextButton_Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(nextButton)

** Please check complete source code to see how things are implemented.

References: