Fustigador
Fustigador

Reputation: 6459

Custom component in Swift

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.

Upvotes: 1

Views: 984

Answers (1)

pbasdf
pbasdf

Reputation: 21536

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.

Upvotes: 1

Related Questions