0

I have a bunch of static objects (UILabel, buttons, views) in multiple Scenes. They are not connected to any IBOutlet. But I'd like to access them at appdelegate (or first VC), and change their properties before it is loaded.

Anyway to do this?

EDIT: Adding my intention: I actually wanted to make a custom "multi-language" app. I want to be able to change language from within the app. I can get a list of all the objects by applying built in localization of storyboard (Main.strings is autogenerated). Then I disable localization again. Then from this autogenerated file, I want to be able to connect it to a json data based on language that I select.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • 1
    You should read this : http://stackoverflow.com/questions/22061402/change-language-in-the-app-programmatically Your intentions does not seem like "clean" or good practice in my opinion. Use the methods already handed to you by Apple. Maybe I misunderstood you what you want to do tho :) –  Apr 06 '17 at 04:02
  • Hey that is nice solution!. But I don't want the user to restart app to have the language changed. – GeneCode Apr 06 '17 at 04:28
  • I have looked into these kind of things before, and I noticed for example Instagram restarts your app too, they shouldve atleast had that solution to avoid the restart (Of course you can do stuff better than others) but having that said I don't think there is a clean solution for this if you are going to use the internal localizations. In other words it is alot of mess to go trough to avoid the restart of the app :P –  Apr 06 '17 at 04:31

2 Answers2

1

Of course you can. For example, you can use tags of UIView. Just set tags in Storyboard. It's easy but not so good. Another way to do this is using Accessibilities. Enable and set for it in Storyboard.enter image description here

And then you can access it by accessibilityIdentifier property.

Community
  • 1
  • 1
Lumialxk
  • 6,239
  • 6
  • 24
  • 47
  • I said without IBOutlet though. – GeneCode Apr 06 '17 at 03:20
  • @GeneCode Otherwise we don't need any other accessibilities. – Lumialxk Apr 06 '17 at 03:23
  • 1
    You can loop through your `view`s `subViews` array looking for the `accessibilityIdentifier` you are looking for. Your views may themselves have subviews, so you may need to search those subviews too; generally an IBOutlet is much simpler. – Paulw11 Apr 06 '17 at 03:24
  • @GeneCode I'm sorry I didn't say you should loop through. Look at Paulw11 's comment. – Lumialxk Apr 06 '17 at 03:25
  • IBOutlet is okay for smaller project I guess. But imagine of an app with 50 scenes, and each scenes has 50 static objects. As programmer, I am lazy to connect one by one. XD – GeneCode Apr 06 '17 at 03:53
  • I added my intention of why i want to do this in my question if anybody have ideas to approach this, pls let me know. thanks – GeneCode Apr 06 '17 at 03:57
1

I will post my choice of "solution". So what I did was make use of accessibilityIdentifier to set the "key" for the multilanguage phrase translation purpose.

And I make use of the UIView+Recursion class (you can find this simple class somewhere in SO), and basically iterate all the objects in a particular Scene and if the text matches, set the key in accessibilityIdentifier property (either in viewDidload or viewWillAppear or viewDidlayoutSubviews).

This way you can have language changes "on-the-fly" within the app, without restarting.

GeneCode
  • 7,545
  • 8
  • 50
  • 85