IOS: What is the correct way to change a view from the class of a different view? - ios

I have a UIImageView subclass (in swift) set up so I can access touchesBegan/touchesMoved/touchesEnded.
When one of these methods is called, I need to change a property of a different, loaded view.
It seems to me that I will now need to access the active view controller in order to set the properties of this other view. Is there are better way to go about this (such as event methods called in the view controller)?
Note that I'm new to iOS and I am not extremely familiar with the event system yet, as most information I've found is written in Objective C and not in Swift. (Don't worry, I'm looking through Apple's Documentation.)
Also, no, I can't change the UIImageView to a UIButton. Even if I changed to a UIButton, I need access to the individual touchesBegan, etc. methods and the same problem would persist.

If I'm understanding you correctly, you need to reference the UIViewController from the UIImageVIew to push/present a new UIViewController. You have a few options:
Fire an NSNotification from the image view and have an event listener on the view controller.
Create a delegate on the image view that fires a selector on the view controller.
Hold a reference to the view controller on the image view, and push a new view controller from the image view with that reference.
Modally present a new view controller on the application window's rootViewController.

Related

Is there any situation where using UICollectionViewController is appropriate compared to using UIViewController + UICollectionView?

I am quite comfortable in using UICollectionView inside a UIViewController.
I have never used UICollectionViewController before.
I was wondering, is there any situation, where using UICollectionViewController is more appropriate?
In addition to the obvious -
setting your UICollectionView up for you
automatically marking conformation for necessary protocols like UICollectionViewDataSource & UICollectionViewDelegate
A UICollectionViewController gives you a few built in nice features that you otherwise have to do a lot of extra work to get right manually.
installsStandardGestureForInteractiveMovement
The default value of this property is true. When true, the collection view controller installs a standard gesture recognizer (based on a long-press gesture) to manage the reordering of views inside the collection view. The collection view’s data source must declare its support for reordering items by implementing the appropriate methods. Setting this property to false prevents the installation of this gesture recognizer.
useLayoutToLayoutNavigationTransitions
This property helps facilitate transitions between two or more collection view controllers using a navigation controller. When configuring your navigation controller, install a collection view controller as the root object on the navigation stack and set its value for this property to false. When the user selects an item that would require pushing a new collection view controller on the stack, set the value of this property for the new view controller to true. When you do that, the navigation controller performs an animated layout change between the contents of the two collection view controllers instead of the traditional push animation. Similarly, popping the topmost collection view controller off the stack animates back to the previous layout. The navigation controller drives the transition between the view controllers, including the ability to drive the transition interactively.
You must set the value of this property before pushing the collection view controller onto a navigation stack. Do not change the value of this property after the view controller is already on the navigation stack.
It's up to you if your case is a good candidate for using these features or not and based on that you can make the call.
UICollectionView inherits from UIScrollView (just another UIView)
UICollectionViewController inherits from UIViewController. and it implements some protocols. like UICollectionViewDelegate and UICollectionViewDataSource. it means everything is already done for you. and you just have to use it.. but as everything is already done for you. you may not be able to do some stuff. like resizing your collectionView.
if you want full control I recommend you to use your own UIViewController.. and add a UICollectionView as a subview.. so you can place it wherever you want and resize it.. don't forget to implement UICollectionView protocols for delegation and datasource.
You may change some properties with your View controllers. But when you use as part of its' controllers, you can't change it.
For example you Cant change UICollectionView Size of UICollectionViewController .

How to Navigate from a UIView class to a UIViewController?

I have a UIView which I am using as a seperate module and can include it anywhere I want to. Now I want to navigate to a UIViewController on click of the button inside the UIView.
Hope this is pretty clear.
Short answer: You shouldn't. You need to read up on the MVC design pattern. A UIView is a view object. You are trying to add controller behavior to a view object.
You should take a look at parent/child view controllers, container views, and embed segues. You could easily create a view controller that manages a "tile" inside another view controller, and sends messages to it's parent when the user taps buttons. This is a very common way of doing things.
The best way to do this is to supply the ViewController to the view as a delegate, conforming to a protocol you create for it.
For instance, if you UIView is used to pick an image, then the delegate should have a method akin to:
(void)view:(UIView*)view choseImage:(UIImage*)image;

View as SubView VS ChildViewController

Can any one explain when should we add a UIViewController as ChildViewController?
What is the benefits of adding it as ChildViewController instead of subView?
Please help me to understand the purpose of ChildViewController.
When you add a view controller as child view controller, the parent view controller will hold a strong pointer to the child view controller so it doesn't get released instantly. This does not automatically add child's view to parent's view. So you will have to call them both.
I only used it when I needed to create multiple view controllers to be inserted in another view controller and didn't need to directly access it.
its all about UI and code management if you are using subview to achieve what you want to implement inside your app you need to code for your view inside same viewcontrollers class but something interesting i found by creating childviewcontrollers.
empowered to work on a seprate viewcontroller will invoked along with its parent viewcontroller along with its seprate class.
infinite controllers that will be updated tapping a button.
Creation of childViewControllers can be achived by implementing containerView.
or you must have a look of this link hope its helpful to understand.

Subclass UICollectionViewTransitionLayout for automatic UINavigationViewController transition animation

Is it possible to subclass UICollectionViewTransitionLayout to change the automatic (e.g. non-interactive) animation when pushing/popping a view controller onto/off a UINavigationController stack?
Apples documentation and some articles in the internet suggest it should be possible:
If you want to provide more than just a linear transition from the old to new layout over time, you need to subclass and provide the layout attributes for items yourself
…but I didn’t find a way yet.
I would like to use UICollectionViewController’s useLayoutToLayoutNavigationTransitions to push one collection view controller on top of another (using the same data source). The two view controllers use different subclasses of UICollectionViewLayout which I want to transition using custom UICollectionViewLayoutAttributes that need to be keyframed and modified by the transitionProgress.
I was hoping, just implementing collectionView:transitionLayoutForOldLayout:newLayout: would do the trick, but that method only is called when transitioning interactively.
The best way I could come up with for the pushing is to call startInteractiveTransitionToCollectionViewLayout:completion: on the UICollectionView and pushing the new view controller without animation in the completion handler. For popping the view controller I would hook into -viewWillDisappear: of the view controller and check for the current view controller in the stack; if it is not there, I could again perform startInteractiveTransitionToCollectionViewLayout:completion:
Somehow I guess there is a better way…

UIContainerView call parent method

I am a very new iOS dev and need your help.
I have a simple app, made from:
1 view controller that contains
3 UIContainerView, each one is linked to its own view controller and class
A view (the player)
Here is an image of my storyboard so it will be easier to understant:
=> I need 10 rep to post the image as an image
What I want is that when I click the play button on the cell inside "View Controller Search" it calls a method or function from the parent (main?) view controller including the url of the file to play.
I have already a working action on the play button, I have found how to get the url from it and printed it using NSLog so everything is fine from this part. My only problem is to find a way to communicate with the main view by sending the url.
If hope I am clear enough, thank you for your time.
You've got two options:
The quick and dirty one is to use the parentViewController property of your contained controller, cast it to the type of the parent view controller and call a method on it.
The right way is to define a delegate protocol and property for your search view controller, and make the parent view controller conform to it. Then, in the prepareForSegue: of the parent view controller, set the parent as the search controller's delegate.
prepareForSegue: will be called three times when your parent view controller is loaded, once for each embed segue that you have defined in the storyboard above. Just like when you push on a navigation controller, this is your opportunity to configure the destination view controller. You can give each embed segue in your storyboard an identifier to help with this process.

Resources