[ MVC in iOS with custom UIViews ]

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.

Answer 1


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.