brownmamba
brownmamba

Reputation: 173

How to make one UIButton, UILabel, & UITextField control all input

Currently I am working on my first iOS app, a ToDo Planner. Here is the layout for it.

Layout

Right now I have it set up in that each line has its own textField, button (checkmarked circle), and label (strike line). Like this:

if(textField == textField1) // if the first text field is pressed
    {
        let image = UIImage(named: "To Be Completed Circle.png")
        button1 = UIButton(frame: CGRect(x: 340, y: 56, width: 30, height: 30));
        button1.setBackgroundImage(image, for: UIControlState.normal)
        self.view.addSubview(button1)
        button1.addTarget(self, action:#selector(self.pressCheck), for: .touchUpInside)

    }

And this:

if(button == button1) // if first button was pressed
    {
        button1.setBackgroundImage(image, for: UIControlState.normal) //set image to be of circle with checkmark
        button1.removeTarget(nil, action: nil, for: .allEvents) //remove old target action
        button1.addTarget(self, action:#selector(self.pressUnCheck), for: .touchUpInside) //add new target action for removing checkmark
        self.view.addSubview(button1)
        textField1.textColor = UIColor.gray //change textfield to a gray color

        label1 = UILabel(frame: CGRect(x : 34, y : 70, width: 200, height: 2)) //add the strike line through textfield
        label1.backgroundColor = UIColor(patternImage: labelImage!)
        self.view.addSubview(label1)

    }

Button1 Label1 textfield1 all correspond to the first line of input. Button2 Label2 textfield2 etc...

The textfields were made using interface builder, the buttons/labels were made programmatically. I was wondering if there is any way (I'm sure there is) to use just one button one label and one textfield to control all user input? The buttons and labels all essentially to the same thing.

Let me know if I'm not too clear.

Upvotes: 2

Views: 447

Answers (3)

Dupinder kaur
Dupinder kaur

Reputation: 198

You should create a nib file of UITableViewCell class where you can design a single cell with Textfield, label, button. And you should use UITableview on storyboard and after that you should load that nib.

Upvotes: 0

Anusha Kottiyal
Anusha Kottiyal

Reputation: 3905

You can use a TableView with custom cells for your each line. A single cell can be designed with your required Label, TextField and Button. You can make it through the storyboard itself using prototype cell in tableView.

Upvotes: 2

Michael Volo
Michael Volo

Reputation: 334

This should really be using a TableView. That would allow you to set up one row of the table with the elements you want and reuse them the way you are asking about.

Upvotes: 0

Related Questions