MVC in iOS with custom UIViews - ios

How do you create custom UIView with its controls in iOS??
Lets say I want Radio Streamer just with Play/Pause buttons and scroller. Also lets say I'll create new model (singleton) for that streaming.
How do I handle View and controlling? Do I create UIView and add actions (IBOutlets) to that UIView, or I need ViewController also? Or should I then just use ViewController + model without UIView? Because I saw they always say model and view should never communicate so I dont think its good idea to have just UIView and actions in it...
I have created Model (singleton) and ViewController (.xib which is view 320x50 with Play/Pause and scroller). I have implemented model and actions inside controller and now when I want to use that controller (so radio streamer) I use this code:
RadioPlayerViewController *controller = [[RadioPlayerViewController alloc] initWithNibName:#"RadioPlayerViewController" bundle:nil];
[controller.view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), 300)];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
Is that good aproach?
Thanks.

way you say and implement is correct you have make model class extend with NSObject class here you create named singleton class that is fine with model class
now about view and controller in objective c this part is combine in objective c with Viewcontroller but if you want you can separated view form view controller class
what you did is perfectly fine if you need some controller with it relevant methods and need to sparted form main parent controller and need to serve as indiviual component that you can use this approch here you create RadioPlayerViewController.
you can also create custom view and than add this view in view controller
for how to create custom view you refer this link http://www.raywenderlich.com/1768/uiview-tutorial-for-ios-how-to-make-a-custom-uiview-in-ios-5-a-5-star-rating-view
this is used when you need some reusable views that used in many screens.
way you implement is good approach for custom component you can make changes in component and easily modify it with out any trouble in parent controller.

Related

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;

PrepareForSegue between UIView and ViewController using Swift

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.

iOS : Load uiview of another view controller

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
}

iOS: Recommended pattern for loadView

If I have a complicated view hierarchy in a UIViewController, when would it be appropriate to factor out the main view into its own class, even though it's not re-usable elsewhere? And if I were to do that, what would the proper event handling approach be for a button on that view - addTarget directly to a button property or delegation through the view class?
I'm having a lengthy debate with a colleague about whether we should always create a separate view class.
(For the purposes of this discussion, let's make the assumption that we want to avoid NIB files at all costs.)
You can create separate views for one view controller. If you want to load a particular view based on certain conditions, then you can have one custom init method to load the view to the view controller as given
- (id)initWithView:(UIview *)view {
self = [super init];
if(self) {
[self setView:view];
}
return self;
}
And if you have different buttons in views, you can write button action methods in that views itself. For getting those actions to the viewcontroller you can write a protocol in views and set viewcontroller instance to delegate and implement those protocol methods in view controller. For differentiating the action, you can set tags for each buttons and according to that you can execute appropriate action in view controller.
MVC should always keep controller small and clean. I ask my teams to always logically separate complex view into small views.
As for adding control, always try to simpler way. Use delegate when it is necessary.

Question about hierachy Views design in iOS

If I've a hierarchy of Views to display in my window (let's say a grid and each grid cell is a UIView with Image + caption), how should I implement it without breaking the MVC design pattern ?
Should I create a MVC for each cell, and then how can I add them to the main MCV and related it to the main view ?
Or should I just create a cell View and add it to the main view with addSubView method ?
Thanks
ps. I've found this example, is this correct ? Do I need to create a controller and then add its view to the mainView and release the controller ?
SimpleViewController *viewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[mainView addSubview:viewController.view];
[viewController release];
http://developer.apple.com/library/ios/#documentation/general/conceptual/DevPedia-CocoaCore/MVC.html
Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data...
A view object is an object in an application that users can see...
A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects...
If you just want to display some content then all you need are views, no need for models or controllers if there's nothing to model and no need to have a controller manage those views.
Even if those views are build from some models there no need to necessarily give each one its own controller object. Instead you might have a grid view with a "datasource" delegate which it can ask for the cells to display, much like UITableView.
What do your grid cells represent? Does it make sense to have a model object capture view-independent behavior and state for that data?
ps. I've found this example, is this correct ? Do I need to create a controller and then add its view to the mainView and release the controller ?
Do not do this, that's a misuse of UIViewController and likely to lead to confusion. If you just want a view object then just create the view object. You can load objects from a nib without creating extra controller object like that. Look at NSBundle or UINib for alternate ways to load objects from nib files.

Resources