Tuesday, 28 February 2017

Dynamic UITextview with button




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

import UIKit

class ViewController: UIViewController {

    var tv1 : UITextView!
    var tv2 : UITextView!
    
    var btn1 : UIButton!
    var btn2 : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // create tv
        tv1 = UITextView()
        tv1.frame = CGRect(x: 50, y: 100, width: 300, height: 36)
        tv1.text = "Default Textview 1"
    
        view.addSubview(tv1)
        // create tv2
        tv2 = UITextView()
        tv2.frame = CGRect(x: 50, y: 150, width: 300, height: 36)
        tv2.text = "Default Textview 2"
        
        view.addSubview(tv2)
        
        // creating button for set data
        btn1 = UIButton()
        btn1.frame = CGRect(x: 50.0, y: 200, width: 120, height: 36)
        btn1.backgroundColor = UIColor.darkGrayColor()
        btn1.setTitle("SetData", forState: UIControlState.Normal);
        btn1.tag = 1
        btn1.addTarget(self, action: "getSetData:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn1)
        
        // creating button for get and set data
        btn2 = UIButton()
        btn2.frame = CGRect(x: 50.0, y: 250, width: 120, height: 36)
        btn2.backgroundColor = UIColor.darkGrayColor()
        btn2.tag = 2
        btn2.setTitle("GetData", forState: UIControlState.Normal);
        btn2.addTarget(self, action: "getSetData:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn2)
        
    }

    func getSetData(sender : UIButton)
    {
        var sendTag = sender.tag
        if sendTag == 1
        {  tv1.text = " hello textview, you changed" }
        else if sendTag == 2
        {
            tv2.text = tv1.text
        }
    }

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


}


No comments:

Post a Comment