How to designed a correct MVC on iOS? - iphone

I have a project where I want to display overlays to locate people on a map. I have difficulties to follow the MVC pattern since I've never practice it.
In a database, I'll save a position (in map coordinates) and the image to identify one person (the image of the overlay).
If 2 or more are at the same position, they are grouped under another "group" overlay. When an overlay is touched, I want to display different information, depending of its type ("group" or "single").
Here is my problem : I obviously need to subclass an Overlay superclass, which have the properties position and image, to redefined the overlayTouched: method. But this code his typically a View code of the MVC pattern, but the overlay is a Model object since it will be saved, so i'm confused.
How should I designed this ?

You should not subclass Overlay to add touch events to it, instead create a new UIView, YourNewView
This YourNewView will "has a" instance of Overlay, and it will delegate all its method that are required from the model to Overlay
For example. if you have a touchesEnded in your YourNewView and you want it to save the overlay to the DB, you would do
//in touchesEnded
[self.overlay saveToDb];
And so on, all the calls will be forwarded to the Overlay Class

Related

Get a custom UIView subclass to follow a UIControl around

I've got a subclass of UIControl which represents a slider switch, with a variable 'percent' which stores the current position of the slider, i.e. 0.0 means the slider is on the left, 1.0 means the slider is on the right. I have two images which look like speech bubbles, and when the user clicks the slider I want to display one of these images (they are the same size but look different, designed so one is displayed if percent <= 0.5 and the other one if percent > 0.5) and have the 'tail' of the speech bubble follow the slider (i.e. the frame is set as a function of the percentage).
I have created a custom UIView for the image which changes the image to display via a boolean parameter. I have tried creating a parameter in the UIControl subclass that draws the UIView along with all the other drawing, but I need to draw it outside the UIControl's rect, which can't be done from inside the UIControl's drawRect: method. What would be a good way of managing these two views and making sure the speech bubble always updates when the percentage value of the UIControl changes?
Sounds like your UIControl should simply be:
Tall enough to contain the bar and bubble view
Transparent (so that you can see behind the part of the view where the bubble "slides")
Or am I missing something?
First off, make sure the continuous property is set to TRUE in your UISlider.
Then, make sure you have an IBAction in place to receive update events from your UISlider. You can make the connection easily in Interface Builder (built into XCode 4) or you can do it programatically using UIControl's addTarget:action:forControlEvents: method.
And lastly, inside that IBAction you can now change the frame of your "speech bubble" UIView (make sure to link that up to your parent view controller as a member in your #interface .h file). Where it appears in relation to the slider is left to you as a homework assignment (cause it's really implementation specific -- or, to put it another way, how it appears is up to each individual app & programmer).
I hope this information helps you out!
Thanks for the answers - it turned out the best way to do it was to declare a delegate protocol for my custom UIControl, and assign the speech bubble view as the delegate. Then I made a method that tells the speech bubble to update location (passing in the location) and called that from the UIControl whenever the position of the control updated.
Probably the simplier way is to add to the UIControl a property pointing to the UIView and move it around in the method(s) where you change the control state. Of course to do that you need to create a class like MyCustomControl that extends UIControl.

Multiple UIViewControllers - Best way to implement this

I'm building an iOS application and I try to determine the best way to implement this :
I have a main UIViewController (MainViewController) that displays a simple view. This view contains a button that let the user add an object (let's say a Circle) to the main view. The user can add multiple circles by pressing the button and move each of them by dragging them. The circle objects should have their own color (randomly chosen).
The question is: what is the best way to implement this?
Should I create an other UIViewController subclass (CircleViewController) for the Circle object, whose view actually draws the circle?
And then, when the user presses the button, should I create a new instance of this CircleViewController and add its view to the MainViewController?
When the user double-tap a circle, it should disappear... How can I send a message to the mainViewController to tell it to remove the concerned CircleViewController's view?
Thank you very much for your help.
If your object is really as simple as a circle you should look at Quartz in Apple's Documentation and the method drawRect: in UIView. If you are doing something more like an image you could subclass UIView and put your code in there. Either way, you do not need to create new viewControllers.

Iphone - Drawing into a view : philosophy and interactions

I've understood that I need to subclass a UIView t be able to draw inside it.
The thing I don't understand yet, is the philosophy of the way i must be done...
Let's say I have a view controller, and depending on context, I may want to draw a line into one of the subviews it manages, or a circle, or a rect, or a processed graphic. Or lets say two points that are moving inside a view into a defined rect and that display a bigger point when they are close.
How may I subclass and define the subview to make it able to do this only into its drawRect method ?
How does the controller, that manages more than this simple UIView (let's imagine you have a view controller that manages a view inside which there are many other view, and you want to make some drawings in two of them), and that knows what is needed to be drawn into the correct view (it's a controller, isn't it ?), may interact with the views ? And when the drawing is done, how may the views interact with the controller ?
I've read many doc about drawings (apple, web, forums, tutorials, ...), but I still can't touch the philosophy of the way this must be done.
it's very simple. Make a new class, OliverView, which is a UIView. (ie, it is a subclass of UIView.) In that view, make it draw stuff in a fancy way, inside drawRect.
Now make a UIViewController, called OliverVC. In storyboard put an OliverView inside OliverVC. (beginner explanation of how to do that).
In the OliverView, have properties "hours", "minutes", "seconds".
Now, in OliverView - in the drawRect - have a fancy way to display those values. (Pie chart, glowing letters, animation - whatever you want.)
Now, up in OliverVC, do some calculations to determine the time in Zimbabwe, for example.
Once you want a time displayed, simply set those properties in OliverView - - and you are done.
Your colleague could be programming the OliverView. You need know nothing about how she is going to display the time. Conversely, your colleague need know nothing about your calculations in OliverVC..
So, it's simpleL One part has the job of displaying the data. One part has the job of coming up with the data (doing whatever sort of calculation is relevant in the app).
It's the only architecture possible in a "real time" screen device where the views can and do change at any time.
In answer to your question below: you've forgotten that quite simply, if you have a button that would be a whole separate element. (Perhaps sitting "on top of" the OliverView.) So, it's easy!
The -drawRect method in your UIView subclass defines the onscreen appearance of the view. All drawing is done in -drawRect. Your UIViewController calls methods on its UIView to tell it to draw something differently or to perform some other action.
The UIViewController manages everything to do with the view that is not inherently associated with the drawing of the content. Data associated with the view is often stored in the controller.

Grasping the concept of UIviews and UIViewcontrollers

A couple of weeks ago i have started my research in Iphone app development and after alot of hello world applications in different settings i am now ready for my very first application based on the MVC design pattern used in Cocoa.
This opens up alot of questions for me though and after reading the different class references about UIViews and controllers i am stuck in trying to figure out which one i should be using.
In my application i am trying to create a grid of small rectangle's with each rectangle having a different text value on them, to be more specific, i am trying to create a simple calender that will display all the days of a month in a grid.
Every rectangle is a instance of a class i named Tile, in this class i want to implement the drawRect method to draw the rectangle for me and set the text value to the day it should represent.
In order to implement this i have done some research on how this should be done.
From what i have learned so far is that UIViewcontrollers do not really display anything, they are basically sitting there waiting to respond to any events from their children.
In my application i would translate this to the Controller that will respond to each touchevent on a tile.
A UIView however is also a container but one for objects that will need drawing methodes like drawRect. This would translate to the grid that will hold all of the tiles if i'm correct.
Except, i have no clue what subclass i should use for each tile, i have the feeling i am really missing some basic knowledge here but i just can't figure it out. Would really appreciate it if anyone could point me in the right direction with this.
If there were any two apple document you should read, it is the one about UIViewControllers which can be found here and the one about UIViews which can be found here. The UIViewController, as you mentioned, is more about integrating with the iOS system than being a visible component. It has a reference to a UIView, and that UIView is the root node in the visible tree of elements which starts at that View Controller.
In iOS programming you don't really need to worry about drawing rectangles, because for the most part you will be extending elements which know how to draw themselves and then just telling them where to go. The basic visible element in this case, is the UIView. There are many different kinds of UIViews (see the graphic in the UIView programming guide link), so for your case you could use a simple UIView with a background image set to your calendar box graphic, and add a subview of type UILabel. UILabel is a subclass of UIView, so you know it will be something visible as well.
Once you grasp these concepts (which can take a long time) Interface Builder will start to make more sense and you can start doing some of these things with it - and understand how its working. In essence it will create the hierarchy of a UIViewController referencing a hierarchy of UIViews automatically, then you.
Tile should be subclass of UIView since you want to drawRect your "days". Then you can add as many Tiles as you want to your UIViewController.view and manipulate them from UIViewController code (.m file).
But you can add UILabels to your Tile view and manipulate them by setting their text property. In this case you won't need to override drawRect: at all, UILabel will do the rest for you, but you will have to programmatically add this labels to your Tile (e.g. in Tile's init method) or in Interface Builder. In the last case you will have to load them from XIB using [[NSBundle mainBundle] loadNib:owner:options:] method.

iphone interface overlay pass-through events

alt text http://www.davidhomes.net/question.gif
I'm farily new to iphone dev (<3 months on my free time) and I'm starting development of my second app.
From the image, I'm adding a number of UIViews as subviews to my main UIViewController.view, the number of Views to add varies based on user selectable data.
Each view contains several controls, a label, a UITextField and a Horizontal UIViewPicker.
For simplicity I put a (VERY ROUGH) mock-up here with only two buttons.
Because I want to improve the GUI, I want to overlay an UIViewImage as the top sub-views of the added UIView, something like in the image.
The question is on passing through the events to the objects below it. I've read somewhere that one way was to use clipping, but the actual shape is more complex than just an oval frame.
Somewhere else I read that one could add four UIImages, one at each border, which would let the events pass through this hole. Seems like a dirty solution to me (Although I'm sure it would work)
Any ideas about the best way to do this? Any links to a tutorial or recipe online?
Your help is appreciated
thanks
david
Have you looked at protocols? You can define protocols for your views or objects, and the users of that object (the subviews underneath for example) can implement the protocol, set itself as the objects delegate and when actions happen they will notified through the protocols. So for example
An AboveView will declare a protocol that declares methods when a certain gesture was senced by that view so something like
-(void)didMakeCircleGesture...
as a property the underneathview will have a delegate, so in your method that actually sence the gesture youll have at the end something like
[delegate didMakeCircleGesture];
in turn the delegate is the view underneath or something, and it will conform to the protocol defined by the AboveView, and as part of it it will have to declare the method didMakeCircleGesture, so as a result when one makes a circle gesture in the AboveView the underneath view that conformed to the protocol will be told of the event and it can take appropriate action