iOS 5.1 UIView to call super ViewController's selector - ios

In my project i have an UIView instance and attached a gesturerecognizer to it. In case that gesture is recognized i want to call its parent viewcontroller to reorganize the scene.
Here's how it's implemented
The UIViewController is called HomeViewController.
It has one subview which is a UIScrollview.
The UIScrollView contains several UIView instances.
To all of these UIView instances i attached a gesture recognizer. When it fires i want to disappear and call the HomeViewController's reOrganizeUI method.
My problem is that i can't reach the HomeViewController from the UIViews.
Is there a way to do this?
Sincerely, Zoli

Surely you can. The simplest way to do this is to add a property to all your UIView subclasses and assign your HomeViewController to that property when creating the views in the view controller's initialization method. Then you'll be able to access the controller from the views directly.

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.

Dealloc of UIView subclass prevents running class methods

I've created a UIView subclass with a UIButton inside with target on tap inside.
The selector is implemented inside the UIView subclass and linked by Interface Builder.
But when I run addSubview in parent view controller tapping button will cause
performSelector:withObject:withObject:]: message sent to deallocated
instance
(I use ARC)
I added some console output in dealloc method and I see that right after viewDidLoad ends with addSubview of the UIView subclass, the sublass is deallocated.
How to prevent that sort of situation?
I tried #property(nonatomic, retain), adding instance to some global array...
But no luck.
I understand that ARC releses the object as there is no strong reference left but I couldn't force to prevent that situation.
Any help would be appreciated.
Creating a strong reference of your subview class in your View Controller class may help you solve the issue.
#property (strong , nonatomic) UIView *subclassName;
Are you storing reference of button (UIButton) you are trying to add as subview?
The problem was in linking NIB to Class.
I linked File owner to class instead of linking the UIView object.
Normally when there is working on nib with UIViewController, there is a need to link File Owner to UIViewController class.
But when working on UIView subclass the UIView object must be linked.

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

IBOutlets for custom UIView

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.

presentModalViewController inside a UIButton subclass

Very new to iOS coding. I'd like to be able to display a modal dialog from inside a UIButton subclass. I've used presentModalViewController from inside a UIViewController before. How would I grab a reference to a UIViewController from my subclassed UIButton?
Can I somehow discover it's parent? Implement a protocol in the parent UIViewController that I can set as a delegate on my UIButton subclass?
You should be able to use any subclassed UIButton just as a normal UIButton. Setup an action that is triggered on the button which will call a method on the view controller. The view controller will deal with pushing the modal view controller.
Although there are ways in which you can find out the view controller from the button when it is attached to a view, this is a better approach.

Resources