I am doing some unit tests on a ViewController that has an MKMapView.
If I want to test if the mapview is not nil, I have to explicitly initialize it, which is unnecessary to otherwise just use the mapview.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface HomeVC : UIViewController <UIImagePickerControllerDelegate, CLLocationManagerDelegate, MKMapViewDelegate> {
MKMapView* mapView;
CLLocationManager* locationManager;
CLLocationCoordinate2D currentLocation;
}
#property (nonatomic, strong) IBOutlet MKMapView* mapView;
#property(nonatomic, strong)CLLocationManager* locationManager;
#property(nonatomic)CLLocationCoordinate2D currentLocation;
#end
#implementation HomeVC
#synthesize mapView, cameraButton,cameraView, locationManager, currentLocation;
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = MKMapView.new;
self.mapView.mapType = MKMapTypeStandard;
}
If I put a breakpoint after the map initialization, it shows the mapView initialized with only a UIView property. It is properly hooked up in my Storyboard file. And works fine, I just can't test any properties on it.
Related
I'm trying to simple alter the image of a pin on the map, but I can't create the annotation due this error.
Theres my "PlaceMark.h":
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface PlaceMark : NSObject <MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordenada;
#property (copy, nonatomic) NSString *titulo;
-(id)initWithTitle:(NSString *)novoTitulo Location:(CLLocationCoordinate2D)location;
-(MKAnnotationView *)annotationView;
#end
My "PlaceMark.m":
#import "PlaceMark.h"
#implementation PlaceMark
#synthesize coordenada;
#synthesize titulo;
-(id)initWithTitle:(NSString *)novoTitulo Location:(CLLocationCoordinate2D)location{
self=[super init];
if(self){
titulo=novoTitulo;
coordenada=location;
}
return self;
}
-(MKAnnotationView *)annotationView{
MKAnnotationView *annotationView=[[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:#"ponto"];
annotationView.enabled=true;
annotationView.canShowCallout=true;
annotationView.image=[UIImage imageNamed:#"placa.png"];
return annotationView;
}
#end
Edit:
Heres where I am instantiating the marker:
//heres the error
PlaceMark *ponto = [[PlaceMark alloc] initWithTitle:#"Ponto" Location:coordinate];
[_mapKit addAnnotation:ponto];
Already tried cleaning and Rebuild
What's wrong?
Thanks :)
I have a problem obtaining the value of a NSString, named latitudeString from another class. Currently I have two view controllers, the first has users coordinates and the second has a MKMapView which show the current user position on a map. The problem is that I want the pin to show the coordinates of the user position in his subtitle but I can't obtain the value of latitudeString and Xcode gives me this error:
Property 'latitudeString' not found on object of type 'ViewController *'
Here's the code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
{
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
CLLocation *newLocation;
NSString *latitudeString;
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) NSString *latitudeString;
#end
#implementation ViewController
#synthesize latitudeString;
CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
newLocation = [locations lastObject];
latitudeString = [NSString stringWithFormat:#"%g\u00B0", newLocation.coordinate.latitude];
}
#end
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <MKMapViewDelegate>
{
}
#end
MapViewController.m
#import "MapViewController.h"
#import "ViewController.h"
#interface MapViewController ()
{
ViewController *firstViewController;
}
#property (strong, nonatomic) ViewController *firstViewController;
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#end
#implementation MapViewController
#synthesize firstViewController;
- (void)viewDidLoad
{
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapView.userLocation.title = #"Posizione attuale";
self.mapView.userLocation.subtitle = [NSString stringWithFormat:#"%#", firstViewController.latitudeString]; // ERROR
[super viewDidLoad];
}
#end
Can someone help me with this problem? Thanks.
Update #1
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocation *newLocation;
NSString *latitudeString;
}
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
{
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation ViewController
CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
newLocation = [locations lastObject];
self->latitudeString = [NSString stringWithFormat:#"%g\u00B0", newLocation.coordinate.latitude];
}
#end
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <MKMapViewDelegate>
{
ViewController *firstViewController;
}
#property (strong, nonatomic) ViewController *firstViewController;
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#end
MapViewController.m
#import "MapViewController.h"
#import "ViewController.h"
#interface MapViewController ()
{
}
#end
#implementation MapViewController
- (void)viewDidLoad
{
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapView.userLocation.title = #"Posizione attuale";
self.mapView.userLocation.subtitle = [NSString stringWithFormat:#"%#", firstViewController->latitudeString]; // ERROR
[super viewDidLoad];
}
#end
The latitudeString property of ViewController is a private property. Only methods of ViewController can access it. If you want it to be a public property you need to move the property declaration to the .h file.
BTW - get rid of all of your calls to #synthesize. As used, they are obsolete. Also get rid of the ivars you defined for the properties. They are also obsolete.
Try this:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
// A public property
#property (strong, nonatomic) NSString *latitudeString;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
// A private property
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation ViewController {
// A private instance variable (ivar)
CLLocation *newLocation;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
newLocation = [locations lastObject];
self.latitudeString = [NSString stringWithFormat:#"%g\u00B0", newLocation.coordinate.latitude];
}
#end
I was using MapView on Xcode, and everything was working fine, but when I added the following line
mapView.delegate = self;
to ViewController.m, I get the error
Assigning to 'id<MKMapViewDelegate>' from incompatible type 'ViewController *const
__strong'
Here is my code:
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation = YES;
mapView.delegate = self; //The line I added that creates the error
// Do any additional setup after loading the view, typically from a nib.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController {
MKMapView *mapview;
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
You need to declare that your class implements the MKMapViewDelegate methods. In your ViewController.h header file, change the line
#interface ViewController : UIViewController {
to
#interface ViewController : UIViewController <MKMapViewDelegate> {
You need to declare that you respond to MKMapViewDelegate calls.
To do this, simply update the header file for the corresponding class (ViewController.h in your example) as follows:
#interface ViewController : UIViewController <MKMapViewDelegate> {
I was playing around with MKMap and some custom delegate it's not working here and i really don't know why :/
Here's my code :
LocationViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#protocol LocationViewDelegate <NSObject>
#required
- (void)didReceiveLocation:(CLLocation *)location;
#end
#interface LocationViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *locationLabel;
#property (nonatomic, strong) id<LocationViewDelegate> delegate;
- (IBAction)sendLocation:(id)sender;
#end
LocationViewController.m
[...]
- (IBAction)sendLocation:(id)sender {
if ([_delegate respondsToSelector:#selector(didReceiveLocation:)]) {
[_delegate didReceiveLocation:_currentLocation];
} else {
NSLog(#"nope");
}
}
[...]
MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "LocationViewController.h"
#interface MapViewController : UIViewController <LocationViewDelegate>
#end
MapViewController.m
[...]
- (void)didReceiveLocation:(CLLocation *)location {
_mapView.centerCoordinate = location.coordinate;
}
[...]
I'm always getting the "nope" message means that respondsToSelector returns NO.
I did pretty the same example a few days ago and everything was fine.
Someone can see where's the problem here ?
Set your MapViewController as the delegate for the LocationViewController i.e. in your MapViewController.m:
self.locationViewController.delegate = self;
Just as an aside, I'm presuming your MapViewController owns a LocationViewController instance, if so it's better to set the delegate property in LocationViewController to 'weak' to avoid a circular reference. #property (nonatomic, weak) id delegate;
i try a simple Delegate but the method won't fire. Here is my code:
The view with the protocol and a button which should trigger the delegate:
ViewController.h
#import <UIKit/UIKit.h>
#protocol InitStackDelegate <NSObject>
#required
-(void)initstack:(NSInteger)amount;
#end
#interface ViewController : UIViewController{
IBOutlet UITextField *amountTextField;
__unsafe_unretained id<InitStackDelegate> delegate;
}
- (IBAction)init:(id)sender;
#property (nonatomic,assign)id delegate;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize delegate;
- (IBAction)init:(id)sender {
//send the delegate
[delegate initstack:[amountTextField.text intValue]];
}
AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate,InitStackDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic) ViewController *myView;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window;
#synthesize myView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//..... push second controller into navigation stack
myView.delegate = self;
return YES;
}
...
#pragma mark Delegate Method
-(void)initstack:(NSInteger)amount{
NSInteger test;
}
#end
When i hit the button i get to (IBAction) init but then the delegate do nothing. I set a breakpoint in AppDelegate.m but it is never reached. Any help?
thx
Mario
In you App Delegate, when you set myView.delegate=self myView doesn't actually point to anything!
You need to set myView to point to your ViewController.
Use this in didFinishLaunchingWithOptions
myView = (ViewController *)self.window.rootViewController;
myView.delegate = self;