I have a UIView in a view controller which displays an image. This UIView is associated to classX, while the view controller it is in is associated to classY.
My question is, how can I use prepareForSegue in the case? I want to send data from my viewController class to the UIView class to display an image depending on the value it receives respectively.
I have it this way because in classX I need to inherit the UIView class to display the pictures, while in classY I need to inherit the UIViewController because i have functions that need it there; from my knowledge, I cannot inherit both together.
Any thoughts on how to do this?
Thanks
Create an IBOutlet for the image view in your view controller so you can access it.
The view controller or any code in prepareForSegue can now access the image view and set its contents using whatever methods your image class provides.
Related
My app header is currently a full width, 65pt height UIView which I then use as a generic header for all pages.
class AppHeader: UIView {
...
}
Then, in my Main.storyboard I have a UIViewController with a View from the object library which has its class specified as AppHeader.
My AppHeader (UIView) has multiple buttons which should, if clicked, take you to from the page/controller you're currently on to another.
From the AppHeader class I do not have access to use the present method to show another controller as its not within scope.
Here is my AppHeader.xib:
How can I resolve this?
This is very bad behaviour, you should not use a UIView as a header. You should add a Navigation View Controller as the first view controller of your app. Navigation view controller has the navigation bar where you can put the buttons you want there. From that ones, you will be able to push or present other view controllers.
Check the official documentation: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
Make your AppHeader view a custom subclass of UIView if it isn't already. Wire the actions on the button to IBAction methods in the view.
Create a protocol AppHeaderDelegateProtocol. Give your AppHeader class a weak delegate property. Define methods in that protocol that let the AppHeader notify it's owning view controller about button presses.
Implement your AppHeaderDelegateProtocol in view controllers that will contain instances of AppHeader.
Connect the delegate property to each instance of AppHeader's owning view controller.
That should do it.
You can create a protocol - call it AppHeaderDelegate or something - and set up your other viewControllers to adopt that protocol. You could define functions to let your delegate know that a certain button was pressed, and your delegate viewController can react to that by presenting the correct view controller.
Apple's docs on protocols here.
Alternatively, you can use NotificationCenter to broadcast notifications to let subscribers know that a certain button was pressed, and have your viewControllers listening for these notifications and reacting to them accordingly. You have to manage when classes start/stop listening, though, as you may have several objects trying to react to a single notification.
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;
Is there any way that could send the value in Class of ViewController to Class of View?
Because I want to make a drawing board and I made a modal scene to set values about color, width .
I know how to send the value in modal scene to my ViewController , but now I need use those value in Class of View , or not Class of ViewController.
Sounds like you need to devise a protocol to open a delegation channel between the two classes. Then when the modal VC wants to send data to its delegate (its presenter, in this case), it can of its own volition.
The View Controller can have a reference to the View, therefore the View Controller can simply pass the values to View by calling a View's method or updating View's properties. This is a common pattern: View defines a Protocol and data/requests from View to ViewController go through this Protocol (the ViewController acts as delegate of View); and ViewController owns the View.
For inspiration on how to implement a farily decoupled design applying this pattern, you can have a look at documentation on UICollectionView and UICollectionViewController.
Other options, depending on what design you need, are Key-Value Observing or Notifications.
I have a UIViewcontroller on which I am showing UIview which contains a registration form.
now I need this same registration form on another UIViewcontroller without recreating the same for second UIViewcontroller.
I am using UInavigationcontroller.
Is there any way to achieve this? I am stuck since last few hours and tried everything.
First method & the one that I will prefer is to create the registration form as a separate UIView class.
Then you can use it in various view controllers as subview.
Second method, you can create a parent class & inherit both the classes from this parent class. This parent class will contain all the common functionalities.(as mentioned by #alinoz).
You can add viewController in another view controller. Create instance of registretionViewController and add its view in current viewcontroller vew. [self.view addSubview:regViewController.view]. And if you want to push this you can simple create instance of it and push in navigation stack.
Let the class of your registration form view be RegisterFormView.
In appDelegate create a class public property of RegisterFormView.
#property (nonatomic,strong) RegisterFormView *formView;
When you first create form view in viewController A, assign the same in appDelegate also.
[UIApplication sharedApplication].delegate.formView = registrationFormViewInstance;
In another view controller(B) where you want to use the same formView,
if([UIApplication sharedApplication].delegate.formView != nil){
//use the form view
}
How can i push a view from a class which is a child of NSObject to the screen/window?
At this moment I'm writing a class to sync the data in my app with the data on my server and i want to use the MBProgressHUD class.
The Problem is, that the sync class is a child of NSObject and I don't know, how to push something out of this class to the screen.
Or should i have edit the NSObject class completely to another kind of parent class?
Thanks,
teawithfruit
I would present the HUD on your current view. If all the work is in your child class, use the -showWhileExecuting method and just call your child class, do all the work, then when it returns back, it will automatically dismiss the HUD.
Make a UIView property, do your graphics with that view, then, from the viewController, add the view property as a subview.