MapView error on iOS - ios

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> {

Related

I need to pass a string from a NSObject class to a UIViewController with a delegate

I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}

Property not found although it's in the header

I have one view and one View Controller. I am trying to have a reference to View Controller from the view.
PlusCalendarView.h
#interface PlusCalendarView : TSQCalendarView
#property (nonatomic, strong) UIViewController *initialVC;
#end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpCalendarView:self.myCalendarView];
self.myCalendarView.initialVC = self; // ERROR: Property initialVC not found on object of type "PlusCalendarView"
}
How come it cannot find initialVC property?
Update:
ViewController.h
#import <UIKit/UIKit.h>
#import <TimesSquare/TimesSquare.h>
#interface PlusCalendarView : TSQCalendarView;
#end
#interface ViewController : UIViewController
#property (nonatomic, strong) NSCalendar *calendar;
#property (strong, nonatomic) IBOutlet PlusCalendarView *myCalendarView;
#end
The problem is that you're declaring PlusCalendarView in ViewController.h AND PlusCalendarView.h. You should remove the #interface declaration from ViewController.h.
Instead, add #class PlusCalendarView in ViewController.h then #import "PlusCalendarView.h" in ViewController.m

MKMapView is not initialized

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.

iOS accessing NSObject class from my viewController

I'm trying to access to another class from my viewcontoller but is not working:
viewcontroller.h
#import <UIKit/UIKit.h>
#class firstClass; //nsobject class
#interface ViewController : UIViewController
{
firstClass *firstclass;
}
#property (retain,nonatomic) LEMZfirstClass *firstclass;
---
firstClass.h:
#import "LEMZViewController.h"
#interface firstClass : NSObject
{
ViewController *viewController;
}
#property (retain,nonatomic) ViewController *viewController;
-(void)doSomenthing;
firstClass.m:
#synthesize viewController;
-(void)doSomenthing
{
viewController.firstclass=self;
viewController.outPutLabel.text=#"This is my Label";
}
viewcontroller.m:
#synthesize firstclass;
- (void)viewDidLoad
{
[super viewDidLoad];
[firstclass doSomenthing];
}
it compiles with no errors but the label is never updated and for that matter the first class is never call it all. What I'm doing wrong? I'll really appreciate your help.
A few things I'm noticing:
Generally you would have the ViewController class handle updating its own UI elements, not another class.
Where is your outPutLabel variable? Is it created by code or an IBOutlet that is wired up in InterfaceBuilder?
Before you can call something on firstclass, you must create it. Something like this:
firstclass = [[firstClass alloc] init];
[firstclass doSomenthing];
The viewController.firstclass=self; line would be redundant then.
Your firstClass.h
#import <Foundation/Foundation.h>
#interface firstClass : NSObject
+(NSString *)doSomenthing; //Instance Class
#end
firstClass.m
#import "firstClass.h"
#implementation firstClass
+(NSString *)doSomenthing
{
return #"This is my Label";
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "firstClass.h"
#interface ViewController : UIViewController
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[firstClass doSomenthing];
outPutLabel.text=[firstClass doSomenthing];;
// Do any additional setup after loading the view, typically from a nib.
}
Note: Here I am using instance class. Before you work this code you must study about Instance class.

ios delegate doesn't fire

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;

Resources