Swift __1 try to write the first Swift program

Source: Internet
Author: User



Reference: http://swiftist.org/topics/96?page=2#comments



Found several places have been error, their own modified under, there may be xcode6 update caused



Code to write a disorderly, this is my mishap, this note is added later, This is my mishap, need to Change. Develop a good code habit.






Put your own code Underneath.


//
// ViewController.swift
// SwiftCounter
//
// Created by thirty-one on 14-8-20.
// Copyright (c) 2014 yetuoxun. All rights reserved.
//

Import UIKit

Class ViewController: UIViewController {
    
    Var timeLable: UILabel?//Show remaining time
    Var timeButtons:[UIButton]? //Set the time button array
    Var startStopButton: UIButton? //Start Stop button
    Var clearButton :UIButton? // reset button
    Let timeButtonInfos = [("1分",5),("3分",180),("5分",300),("秒",1),]// Array Recording Options
    Var remainingSeconds: Int = 0{
        / / Real-time monitoring of remainingSeconds changes through the willSet method to change the UI (personal understanding willSet need to verify)
        willSet(newSeconds){
            Let mins = newSeconds/60
            Let seconds = newSeconds%60
            self.timeLable!.text = NSString(format: "%02d:%02d", mins,seconds)
        }
    }
    
    Var isCounting :Bool = false{
        / / Real-time monitoring of isCounting changes through the willSet method to change the UI (personal understanding willSet need to verify)
        willSet(newValue){
            If newValue{
                / / Start timer
                Timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
            }else{
                / / Destroy the timer
                Timer?.invalidate()
                Timer = nil
            }
            / / Change button clickable status
             setSettingButtonsEnabled(!newValue)
        }
        
    }
    
    / / Change button clickable status
    Func setSettingButtonsEnabled(enabled: Bool) {
        For button in self.timeButtons! {
            Button.enabled = enabled
            Button.alpha = enabled ? 1.0 : 0.3
        }
        clearButton!.enabled = enabled
        clearButton!.alpha = enabled ? 1.0 : 0.3
    }
    
    / / Declare timer
    Var timer: NSTimer?
    
    //override What does it mean to ask for a certificate?
    Override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.grayColor()
        / / Initialize data and UI
        setupTimeLable()
        setuptimeButtons()
        setupActionButtons()
        // Do any additional setup after loading the view, typically from a nib.
    }

    Override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

/ / Initialize the display time lable
    Func setupTimeLable(){
        timeLable = UILabel();
        timeLable!.textColor = UIColor.whiteColor()
        timeLable!.font = UIFont(name: "", size: 80)
        timeLable!.backgroundColor = UIColor.blackColor()
        timeLable!.textAlignment = NSTextAlignment.Center
        self.view.addSubview(timeLable!)
        
    }
    

    //Initialize Select time button
    Func setuptimeButtons(){
        Var buttons:[UIButton] = []
        For (index,(title, _)) in enumerate(timeButtonInfos){
            // Upstream index title is a variable title similar to temp in for(id temp in arr)
            //Initialize button similar to alloc] init
            Let button : UIButton = UIButton()
            Button.tag = index;
            / / Set the title
            button.setTitle("\(title)", forState: UIControlState.Normal)
            / / Set the background
            button.backgroundColor = UIColor.orangeColor()
            / / Set the color of each state of the title
            button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
            
            //Button binding time Trigger method
            button.addTarget(self, action: "timeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            / / Add button to the buttons array Personal understanding Need to require
            Buttons.append(button)
// buttons.insert(button, atIndex: index)
            //button join the current View
            self.view.addSubview(button)
        }
        / / Array pass assignment
        timeButtons = buttons
    }
    
    
    Func setupActionButtons(){
        startStopButton = UIButton()
        startStopButton!.backgroundColor = UIColor.redColor()
        startStopButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        startStopButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
        startStopButton!.setTitle("start/stop", forState: UIControlState.Normal)
        startStopButton!.addTarget(self, action: "startStopButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(startStopButton!)
        
        
        clearButton = UIButton()
        clearButton!.backgroundColor = UIColor.redColor()
        clearButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        clearButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
        clearButton!.setTitle("reset", forState: UIControlState.Normal)
        clearButton!.addTarget(self, action: "clearButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(clearButton!)
    }
    
    
    Func startStopButtonTapped(sender:UIButton){
        isCounting = !isCounting
        If isCounting {
            createAndFireLocalNotificationAfterSeconds(remainingSeconds)
        } else {
            Println("111111111111111")
            //Knock this code time. 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.