IBOutlets for custom UIView - ios

I have a simple application, in which there is a view controller which is inited with nib file. In this nib file I have a simple View who's parent class is a customUIView subclass of UIView. In this view there are some buttons which I want to access on my custom UIView subclass methods when the setNeedDisplay method is called. Is it good practice to create IBOutlets for these buttons in both customUIView and customUIViewcontroller classes, or should I access these buttons by iterating on self.subviews?

You should create the IBOutlets indeed... since this is the best way to acces these buttons properties.

Related

How to make a xib subview interact with superview in Swift?

I've been trying this for days...
I want to have a nib subview with some buttons and other stuff. The main view would have other subviews that change in response to pressing the buttons of the nib view.
What I can do:
I already loaded the xib subview into the superview using this approach.
The problem:
I can't make the buttons on the xib interact with other subviews (e.g. to change the text of a Label)
What I haven't nailed yet is how to structure the files and their relations.
You could use addTarget:action:forControlEvents: after you load the nib to add the action methods for your buttons; the target should be self (the controller where you've added this view). The view should have IBOutlets for any buttons or other controls you need to interact with. You shouldn't have any actions for the buttons in the subview class if you use this approach.
Another approach, would be to have the action methods for the buttons in the subview class, and have those methods call methods of a delegate protocol that you create in the subview class. When you add the subview to your view, you would set the controller as the delegate of the subview, and implement any protocol methods that you defined.

Xib for custom UIView or UIView with class set to this custom UIView

I'm developing an iOS app with latest SDK.
I have created a custom UIView and I will need to add some controls to that UIView. I'm also using storyboards and I have to add this UIView to it.
My doubt if I have to create a xib for this custom UIView or it is better to add an UIView to the storyboard and change its type to my custom UIView.
And I have to get access to those control that I will add to the xib from custom UIView.
What do you think?
What i do/did
Create a custom class as a subclass of UIView
Everything is done programatically and initialization of variables is
done with -awakefromNib
In storyboard add the UIView
Change its custom class to my custom class name.
Execute
and of course you can create a nib for the view and add controls and set connections to it and can be loaded from the nib name

How to add UIViewController as target for a programmatically added subview like in IB

When adding UIViews in Xcode's interface builder you can use Ctrl+drag to create a target-action to the UIViewController that holds that view. You can also do that to any subviews you add using IB.
If a subview is created and added programmatically in awakeFromNib is there a way to add the target-action from it to the view controller right there or do you have to first create the subview in awakeFromNib and later create the target-action using the view controller as a delegate?
If its created from scratch, you have to bind events manually.
If however it's loaded from another NIB, there're at least 2 options:
You can get a uiviewcontroller from view, see this answer: https://stackoverflow.com/a/3732812/126995 Then you can pass the VC to the loadNibNamed:owner:options: method.
In the IB, you can change the type of the root view from UIView into your custom UIView-derived class, and bind actions and outlets from the subviews to the NIB's root view.

Modify and redraw another UIView from the UIViewController

I have a flow issue in my iOS app from my subclassed UIView to its parent UIViewController.
Basically, I have a nib called preferences. It contains two sliders, two labels, and another UIView that will display a shape dictated by the two scroll bars (Stroke and opacity). I successfully painted the subclassed ui to the screen by setting the custom class of the UIView to a separate UIView we'll call subView. I have setters/getters for the scroll bars and they print out their values. How do i let the uiview class (pointed at the ui nib object) to update and redraw since it isn't referenced in the parent preferences class? I tried syntax like this:
[code]
IBOutlet SubClassUIView *subclassUI
[/code]
to no effect. It seems best to point a custom class at the UIView.
Any suggestions and advice would be much appreciated.
TL;DR can't modify subclassed uiview from "parent" uiviewcontroller
Sorry folks, i figured it out.
In addition to passing in a custom class that shares the same type as the IBOutlet (such as a UIView),
there MUST be a link referencing every object in in the .xib to its parent. In this case, the parent UIViewController needed a reference from the custom class ui to a IBOutlet UI. From there, some simple casting from generic and boring UIView to the custom class's unique methods makes for a complete flow.

On iOS, if we use Interface Builder for the UIViewController's view, how do we implement a drawRect for it?

If we use Interface Builder to edit the content in the view, then how do we add a drawRect to that UIView object? (Probably we want a new class FooView that subclasses UIView -- but how will the .xib content be placed on this view?)
Adding "content" to the view controller's view in the xib just involves adding subviews - these are not affected by drawRect:.
That said, to set a custom class as the main view property of a view controller, just select the view in the xib, go to the identity inspector, and change the class from UIView to your custom class.
At beginning i want to point that i have done something like this(subclassing) with UIButton
and since UIButton is inherited from UIView this must be possible with UIView too.
(after subclassing UIView) create an instance of your subclass on your view controller and add this instance to ViewController as subview. At this point you can call your specific drawRect: method, which you have declared on your subclass

Resources