[MapKit]implementing a delegate method - mapkit

the regionDidChangeAnimated is a delegate method which gets called instantaneously.
I want to set a BOLLEAN variable to NO when this method will be called, so i implement it :
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
shouldAdjustZoom=NO;//this is the variable
}
am i right ??

This method is called when the region has been changed, if you add a parameter, xcode will not find the method and will not enter it because it doesn't have the good name.
I think so.

Related

Why using NSNotification argument

Here is the code in main.m
[[NSNotificationCenter defaultCenter]addObserver:logger selector:#selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];
here is the code in .h file
-(void)zoneChange:(NSNotification *)note;
Can someone tell me why zoneChange method take NSNotification as an argument?
How do we know what argument does this method take when trying to declare it to be used by the method mentioned in the main.m file above?
Also I did some research on the class reference and found out this for the selector parameter
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).
Please explain. Thanks
Imagine you have a controller that has two table views:
#interface MyController : NSViewController <NSTableViewDelegate>
#property(nonatomic,weak) IBOutlet NSTableView *tableView1;
#property(nonatomic,weak) IBOutlet NSTableView *tableView2;
#end
And controller is set as delegate for both table views. Now you want to track changes in selections of both tables. You implement tableViewSelectionDidChange: delegate method, but when it is called, how do you know which table view did change it's selection? Here comes the notification argument. It's object property points to table view that did sent this notification, and you can easily differentiate between two. Also it may contain userInfo dictionary, for example NSTableViewColumnDidResizeNotification contains a NSTableColumn and NSOldWidth keys in there.
In described case, the method responding to notification was called under delegate idiom, but in other scenarios (observed notification, target-action, etc.) you also sometimes must differentiate objects that caused a method call.
#implementation MyController
...
- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
if ([notification object] == self.tableView1)
NSLog(#"selection changed in table 1");
else if ([notification object] == self.tableView2)
NSLog(#"selection changed in table 2");
else
NSLog(#"selection changed in unknown table (???)");
}
- (void)buttonDidClick:(id)sender
{
NSLog(#"some button did click. which? %#", sender);
}
#end
Well, this is what the api states
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
where notificationSelector is a
Selector that specifies the message the receiver
sends notificationObserver to notify it of the notification posting.
The method specified by notificationSelector must have one and only
one argument (an instance of NSNotification).
So, most of the time, you would go and read Apple provided documentations to see what kind of arguments are available for your selector.
Source: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Is it possible to replace method of all objects using it as implementation of a protocol?

Is it possible to replace method of all objects using it as implementation of a protocol?
The method is canPerformAction:(SEL)action withSender:(id)sender:
- (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender {
return NO;
}
The initial problem: can't disable "Cut, Copy, Paste" and "Select, Select All" popup menu on UIWebView. Some strange object generates them on its canPerformAction method. And I can't find this object to replace method only on its class.
Very hope to your help. Can't solve this problem for a long time. All other methods to disable selection also disables user input on my page. And I may only replace canPerformAction method of correct or all classes to get it working.
Create a category for class NSObject and swizzle its method.
#interface NSObject (myCanPerformAction)
- (BOOL)myCanPerformAction:(SEL)action withSender:(id)sender;
#end

Can someone explain how I might use this function call that includes a delegate?

This is the prototype:
- (void)startDownloadingDataOfType:(NSString *) type fromURL:(NSURL *) url delegate:(id <GetURLAsyncDelegate>) delegate;
There is a delegate set up with methods such as URLDidFinishDownloading and so on. However I stil don't totally understand delegates - I get their point, but I don't really know how to use them.
This function seems to contain a parameter to pass IN a delegate, but surely I want to extract one?
In the class where I want to call this function I essentially want to be able to trigger a method when the URL has finished it's download. What is the syntax for using this kind of function in a class?
Passing the delegate to the prototype will cause the method to be called on the supplied delegate.
If you want the method (URLDidFinishDownloading) to be called when the download is complete in the class you called it from, implement the delegate in that class and specify the URLDidFinishDownloading method.
Something like below - (note: my obj-c isn't the greatest, but hopefully you get the idea)
#interface MyClass : BaseClass<GetURLAsyncDelegate> {
}
#implementation MyClass
-(void)URLDidFinishDownloading {
...
}
-(void)downloadData {
[object startDownloadingDataOfType:#"..." fromUrl:... delegate:self];
}

How to return a value from an instantiated object in objective c?

I created an object that receives a coordinate and suppose to return a placemark after reverse geocoding it. Problem is that this method is a-synchronic, so I need a way to return the placemark once it was found. In my view controller I call this:
[getzip reverseGeocodeCurrentLocation:coordinate];
where getzip is an instance of an object that implements this (which is still empty in my case....):
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
Now, once I get into reverseGeocoder:didFindPlacemark, how can I return the placemark back to the viewcontroller?
I tried to do the same thing as I saw in some examples being done with LocationManager, where a locationUpdate method is being called from locationManager:didUpdateToLocation:fromLocation, and implementing the locationUpdate in the viewcontroller, but it did not work. In other words, I did that:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
[self.delegate setPlacemark:placemark];
}
and implemented setPlacemark in the viewcontroller, but it doesnt get to setPlacemark from some reason (BTW, the placemark is correct once I call setPlacemark:placemark within reverseGeocoder).
Thanks!
As per your comments, self.delegate is nil when [self.delegate setPlacemark:placemark]; in reverseGeocoder:didFindPlacemark: is executed. Therefore, nothing happens.
You must set the delegate property to your view controller before reverseGeocoder:didFindPlacemark: gets called. If the variable getzip in your view controller is an instance of the class that implements reverseGeocoder:didFindPlacemark: (GetZipCode?), you would do this in your view controller:
getzip.delegate = self;
As I said in my comment, by making your view controller conform to the GetZipcodeDelegate protocol means your view controller can be the delegate because it responds to the messages that the object that is delegating will potentially send to its delegate. It does not mean that it is the delegate.

Get the object which called a method

If I have a call from within a random class like this:
#implementation SomeClass
- (void) classMethodFoo
{
int a = [SomeSingleton sharedInstance].aValue;
}
#end
Inside SomeSingleton sharedInstance, is there a way to get a reference to the object which called this method (without the called passing self as a parameter of course)?
No, information about the caller isn't passed along automatically.
This is why IBAction methods, for instance, have a sender parameter, and why delegate methods often have a parameter that refers to the delegate's object.