-3

I want to access UIElements (e.g Labels and TextFields, etc) of viewController without making IBOutlet connections.

e.g. I have a UITextField in viewController and I want to access it like viewControllerName.textFieldName.text = "something I will set here" or as the same concept of Android findViewById("id of element") I have used "Tags" but it does not meet my requirement.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

2 Answers2

0

You will create UITextField programmatically, and access the UITextFieldName.text = "Some" programmatically.
Create a label programmatically Check This link

Ramprasath Selvam
  • 3,868
  • 3
  • 25
  • 41
0

Yes, you can access UIElements(e.g Labels and TextFeilds etc) of viewController without making IBOutlet connections. First give UIElement a Tag. Then try this. hope this would help:

If you want to access it in the same viewController

let label = self.view.viewWithTag(4) as? UILabel
label?.text = "Hello there"

and if you want to access it from other viewController in firstViewControler

     import UIKit
        internal weak var FirstViewController: ViewController?

//in viewDidLoad
        override func viewDidLoad() {
            super.viewDidLoad()
            FirstViewController = self
        }

it will make it accessible in all other viewControllers then in secondViewController

let newlabel = homeViewController?.view.viewWithTag(4) as? UILabel
    newlabel?.text = "new change"
Govind Kumawat
  • 1,562
  • 1
  • 10
  • 17
  • 1
    Thanks for your ans. But i dont want to use tags – Muhammad Qasim Dec 26 '17 at 07:29
  • you wrote i have used tags so i guess this will help with that – Govind Kumawat Dec 26 '17 at 07:33
  • without IBOutlet connections or tags a UIElements has no identity and without any identity you can't access them – Govind Kumawat Dec 26 '17 at 07:57
  • Are they accessible with auto generated id in source code of storyboard – Muhammad Qasim Dec 26 '17 at 08:03
  • by itself storyboard create id for each UIElements but its only for storyboard under their own tag (like for e.g Labels – Govind Kumawat Dec 26 '17 at 08:29
  • tag="8" and storyboard use these id to let viewcontroller to access them – Govind Kumawat Dec 26 '17 at 08:31
  • any other options.. i dont want to use .swift files anymore – Muhammad Qasim Dec 26 '17 at 09:04
  • Try saying WHY you don't want to use an IBOutlet or a tag. Now you say you don't want to use .swift files anymore. Do you mean you want to store your code in some other kind of file? What exactly are you trying to achieve? – Upholder Of Truth Dec 26 '17 at 15:48
  • You can get an element by its tag. If you are in a view controller code and you need to get a button that you tagged as 123 in the interface builder, you can use this code: UIButton *button123 = [self.view viewWithTag:123]; The element does not need to be a button - it can be any UIView descendant: a label, a text view, a stepper, or anything else. Please note that a more idiomatic way of accessing elements that you create in the interface builder is through IBOutlets. there isn't any other options. – Govind Kumawat Jan 03 '18 at 09:00
  • does your problem still exists? – Govind Kumawat Feb 03 '18 at 17:15