1

I have a storyboard that I want to reuse many times in my app. You can see it here.

The problem is, I don't know how to use that xib to create a custom component to use it. I will need many instances of that component, and once you click on the UIImageView in the right, or in the label, it will get you to a different scene, depending on which individual instance you clicked. Also, the left UIImageView will be different in every instance.

I have done similar in Android, and now I need to do it in iOS, with Swift.

Ive searched, and almost everything I found its in Objective-C. Does anybody knows about a good tutorial to do it in Swift?

Thank you.

EDIT

More elaborated explanation:

1) I have a scene where I need to put that component, as a whole, four times.

2)In every individual component, I need the image view in the right (the one with the white arrow) and the label to be clickable.

3) In every individual component, the click in the image or the label, will lead to a different screen in the app.

4) In Android, it looks like this. The label and the left ImageView are not seen, since they are set programmatically, but they are there. In the upper component, the click leads to an Activity. In the second one, the click leads to a different Activity, and so on.

Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • Can you explain your question a little bit? It is wee bit confusing. If you are asking that how to reach the VCs in storyboard, maybe you could create custom segues. Read answer from Jean Phillipe here http://stackoverflow.com/questions/9674685/creating-a-segue-programmatically – NSNoob Sep 23 '15 at 07:39
  • I think what you are looking for is a `UITableView` with custom `UITableViewCell`s. There must be hundreds of tutorials, even in Swift. – pbasdf Sep 23 '15 at 07:41
  • No, I don't need a table. I need that component... – Fustigador Sep 23 '15 at 07:48

1 Answers1

1

From the images and your description, I would still recommend UITableView and custom UITableViewCells. But if you prefer not, ...

To create instances of your component from the XIB file, use the NSBundle "loadNibNamed" method:

    let viewArray = NSBundle.mainBundle().loadNibNamed("ClickableComponent", owner:self, options: nil)
    let clickableComponent = viewArray[0] as! UIView // or your subclass

You can then tailor programmatically to load the correct images and assign the associated actions.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • The problem with table is that not always my component will be below another. In some screens will there be only two, one at the top of the screen and other at the bottom, etc. I will try your solution. – Fustigador Sep 23 '15 at 09:20
  • I did that, and I see nothing in the storyboard. If I run the project, nothing is seen in the Simulator. Is this even possible in iOS...? – Fustigador Sep 23 '15 at 11:28
  • @Fustigador Sorry, I'm slowly realising what you are trying to achieve. I'm not familiar with it, but I think you need to use IBDesignable and IBInspectable (see NSHipster [here](http://nshipster.com/ibinspectable-ibdesignable/) and a youTube example [here](https://www.youtube.com/watch?v=L97MdpaF3Xg)) – pbasdf Sep 23 '15 at 11:58
  • Thanks for you help, pbasdf. Will read, and watch, later. Im almost out of work by now, but if it works I will upvote the answer above. – Fustigador Sep 23 '15 at 12:11
  • 1
    @Fustigador See also [this SWIFT example](http://iphonedev.tv/blog/2014/12/15/create-an-ibdesignable-uiview-subclass-with-code-from-an-xib-file-in-xcode-6) – pbasdf Sep 23 '15 at 13:19
  • You absolutely saved me, thank you very much, following that tutorial I had the custom component working. – Fustigador Sep 24 '15 at 07:51