Tuesday, 28 February 2017

Dynamic button - 1

//
//  ViewController.swift
//  ImageView
//
//  Created by Divakar Singh on 2/27/17.
//  Copyright (c) 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var label1 : UILabel!
    var btn1 : UIButton!
    var btn2 : UIButton!
    var btn3 : UIButton!
    
    /*
    - Button - 1 is use here for single click
    - Button - 2 and 3 is for multiple button click same function
    */
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // creating text
        label1 = UILabel()
        label1.frame = CGRect(x: 100, y: 100, width: 500, height: 60)
        label1.text = "Default Text"
        view.addSubview(label1);
        
        // creating button-1
        btn1 = UIButton()
        btn1.sizeToFit()
        btn1.frame = CGRect(x: 150, y: 150, width: 120, height: 40)
        btn1.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        btn1.layer.borderWidth = 2
        btn1.layer.cornerRadius = 10
        btn1.setTitle("Button 1", forState: UIControlState.Normal)
        // or can use : btn1.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn1.backgroundColor = UIColor.orangeColor()
        btn1.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Right
        btn1.addTarget(self, action: "buttonClicked1:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn1)
        
        // creating button-2
        btn2 = UIButton()
        btn2.sizeToFit()
        btn2.frame = CGRect(x: 150, y: 200, width: 120, height: 40)
        btn2.setTitle("Button 2", forState: UIControlState.Normal)
        // or can use : btn2.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn2.backgroundColor = UIColor.orangeColor()
        btn2.tag = 1
        btn2.addTarget(self, action: "buttonClicked2:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn2)
        
        // creating button-3
        btn3 = UIButton()
        btn3.sizeToFit()
        btn3.frame = CGRect(x: 150, y: 250, width: 120, height: 40)
        btn3.setTitle("Button 3", forState: UIControlState.Normal)
        // or can use : btn2.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn3.backgroundColor = UIColor.orangeColor()
        btn3.tag = 2
        btn3.addTarget(self, action: "buttonClicked2:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn3)
        
    }

    func buttonClicked1(sender : UIButton)
    {
        label1.text = " Hello Button Clicked event"
    }
    
    func buttonClicked2(sender : UIButton)
    {
        var sendbtn : UIButton = sender
        if sendbtn.tag == 1
        { label1.text = " tab button 1.1 " }
        else if sendbtn.tag == 2
        { label1.text = " tag button 1.2 " }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


No comments:

Post a Comment