Send data from chosen Annotation (InfoBoule button) - iphone

I have a mapView which loads annotations with the help of a web service. I parse the data in json in an array and get the values of each annotation.
But now, I want to send the information of the annotation in selection.
As I can think, It mar rather be like the section in UITableView called DidSelectRow...
How I can do this?

If you mean when the user taps one of the annotations in mapView, you have a delegate method for that. From MKMapViewDelegate Protocol Reference
mapView:didSelectAnnotationView:
Set your view controller as delegate for your mapView and then just use this delegate method to be notified when the user taps on one of your annotations.
Link to Apple Documentation on MKMapViewDelegate Protocol

Related

update location from another view controller

I want to continue to update userLocation every 0.5s from viewController to anotherViewController!! I've tried using a segue but that only updates one, anyone know another method or how?
Thanks!
Sure.
Create a protocol. Let's call it MyLocationDelegateProtocol.
Give it a method to get location updates.
Have your anotherViewController implement the method, and add the MyLocationDelegateProtocol to the class definition. (These steps can be referred to as "making the class conform to the MyLocationDelegateProtocol".)
Give the view controller that will be sending out location updates a locationDelegate property. When you create the view controller, set it's locationDelegate property to your anotherViewController.
Write the location methods so that they check to see if the locationDelegate property is nil. If it's NOT nil, send location messages to the locationDelegate using the message(s) defined in the MyLocationDelegateProtocol.

what's the events of getting and losting focus in a view controller

There are 2 forms in my program. When view controller A gets the focus and be active, view controller B will lost the focus and be inactive. Can anyone show me What the events are?
Thanks
Miken, it depends on what type of objects you are using for your "forms".
The simplest "form" to use is a UITextField, and in that case, events will be sent to your UITextField's delegate. In a lot of simple cases, you will designate the viewController that holds your UITextfield to be the UITextFieldDelegate. For more information on the methods that the delegate has, take a look at this: https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intf/UITextFieldDelegate
In this case, when the text field gets the focus (ie the user clicks on the text field to edit it) the delegate methods:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
will be called. I'm using this as a basic example, and not assuming too much here, but generally you should be looking into your "forms" delegate methods.

What exactly does delegate do in xcode ios project?

I have just been learning iPhone apps development but I have a hard time in understanding what delegate actually means? Can anyone tell me with example what it does and how important it is? Thanks for any helps!
It's a key concept to understand conceptually so getting your head around how to think about it ahead of the technical details is important. Simply put, a delegate is a callback.
Two main scenarios to use delegates:
A class or control wants to abstract out the details on how to do work (like retrieve data).
Allow others to hook code into a pipeline.
Examples:
UITableView - a table view is just a control that knows how to render a list of cells. It handles all the heavy lifting of rendering, scrolling, etc... But, it has no idea how to load your data. So you implement a datasource delegate which has methods to get the cell data for a given row etc... That makes it easy on you. You just use the control and plug in the specifics for your data. The UITableView will do everything for you ... just answer a few specific questions for. A delegate answers those few specific questions.
A text control - you add a text control to your view and voila! you can type in it and alls good. But what if you want to do something when they start typing or when they're done typing? Well, the text control offers a delegate with methods that allow you to hook into the execution pipeline of the text control. It allows the text control to do everything for you and allows you to interject code where you need it. Many times, there's way to interject code to make a decision on whether something is allowed. The control will call back and ask, should I be able to do x? You can interject code and influence the behavior.
If you're creating a control or class, you can create your own protocol, datasource delegates etc... so your control can focus on doing what's advertised. For example, let's say you wanted to create a task control. You could:
First, create a contract. Hey, if you're going to provide data for my control, these are the questions I'm going to ask you. I'll take it from there... In this case, I'm going to ask you the number of tasks and I'm going to have you give me a task given the row number.
#protocol XXTaskBoardDelegate <NSObject>
-(NSInteger*)getTaskCount;
-(XXTask*)getTaskForRow:(NSInteger*)rowNumber;
#end
In the control or class, give the consumer a way to give us the delegate datasource class that will answer the questions the control will ask. At this point, the control is a pure control. It knows nothing about how you get your data. It's asking for an object (id) that implements a contract/protocol. id
#implementation XXTaskBoard
- (void)setDelegate:(id<XXTaskBoardDelegate>)newDelegate
{
// the control stores the delegate so it can callback and ask you questions.
}
Then, for the delegate class, in the header declare you implement that formal protocol
and in the implementation m file you provide the code.
#interface AppController : NSObject<XXTaskBoardDelegate>
{
//...
}
then, implement it in the implementation
#implementation AppController
- (NSInteger*)getTaskCount
{
return [model queryTaskCount];
}
- (XXTask*)getTaskForRow:(NSInteger*)rowNumber
{
return [[model tasks] getItem:(NSInteger*)rowNumber];
}
A delegate is an object that another class can pass messages to. In practice delegate classes have to conform to a delegate protocol.
For instance we will take a subclass of a table view controller. This is a delegate for your table view. First you define that it is a table view delegate by doing this:
MyTableViewController : UITableViewController <UITableViewDelegate>
This says that class MyTableViewController is a subclass of UITableViewController and CONFORMS to the UITableViewDelegate protocol.
Setting [tableView setDelegate:self] (or defining it as such in IB) then passes the self object to the tableview in order for the tableview to send messages to it.
The main message it sends is the didSelectRowAtIndexPath message which tells your class that the user has pressed a table view cell.
So the object that takes the click event (the table view) passes on the message that the cell has been clicked to the delegate object (which in this case is your MyTableViewController object).
Delegate protocols exist so that you can make sure that the delegate object has the necessary methods to deal with your messages. Methods in a delegate protocol can be #optional or enforced. Any methods that are optional don't have to be defined. In your MyTableViewController class the method didSelectRowAtIndexPath is optional - you don't have to have it. If the table view doesn't find the method it just won't call it.
However the cellForRowAtIndexPath is necessary and without it your app won't compile.
I hope this helps and is straightforwards for you. If you need any more info let me know.
Delegates are just way of getting callbacks from something. You pass a delegate (a pointer to an object that conforms to a protocol) to something and when it has new data for you or when an event occurs that something make a method call on the delegate.
For example, when events occur, like your app is put into the background or the app is about to terminate the UIApplication object will call your application delegate to let it know. When a CLLocationManager has a new GPS position is will call your delegate to pass it the new position. UITableViews call their delegates to get UITableViewCells to display in the table. There are many uses of delegates in iOS.

Handle Onclick(on tap) event for iPhone Map Annotations

I have plotted annotations in iPhone Map, now I need to handle the onclick/ontab event for the annotation. Please provide the ways to do that.
Thanks,
Chandra
When annotation gets selected map view calls mapView:didSelectAnnotationView: method in its delegate, so to handle tap event you should implement that method in your map delegate.
First you should give title to pins.If you give then only the delegate methodmapView:didSelectAnnotationView:. tou can handle now.

How to Notify View Controller that a Property of an Object has Changed

I have a view controller which gets an NSObject conforming to a protocol and then displays a view with the object's properties.
The question : How can my controller know that a property of this object has been modified and then refresh the view ?
Thanks a lot
Thierry
There are three ways of doing this:
Have the object call a method in the controller in response to an event e.g. a user clicking the button. This is usually done using an IBAction.
Set the controller to be the delegate of object e.g. a UIWebView sends a message to its delegate when it finishes loading a page.
Use a notification. The object generates the notification and then one or more objects (including the controller) registers to listen for the notification. This is usually not used with interface elements although it can be.
I can't tell you more without more detail about the specifics of your project.
Your viewcontroller should conform to your .
In your model, all your set methods should trigger appropriate functions you define in your modelchangedprotocol.
This OO design pattern is also known as "Observer" design pattern.