1

I am new to programming, and i decided to start off with using Xcode and Obj-C.

I'm making a tabbed application where the second view controller contains the map that is supposed to zoom to the users location. I guess i just cant get the code right, when i try to run the app in iOS simulator it just crashes when i open the second view controller.

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));



    }
}

Within @autoreleasepool i get a "Thread 1: signal SIGABRT" on every run.

This is the output i get when i open the second view controller

2013-08-05 21:31:26.271 placerate[56730:c07] Couldn't find default.styleproto in framework
2013-08-05 21:31:26.277 placerate[56730:c07] *** Terminating app due to uncaught exception              'NSUnknownKeyException', reason: '[<UIViewController 0x98a1120> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mapView.'
*** First throw call stack:
(0x16b3012 0x14d8e7e 0x173bfb1 0xf84e41 0xf065f8 0xf060e7 0xf30b58 0x63a019 0x14ec663    0x16ae45a 0x638b1c 0x4fd7e7 0x4fddc8 0x4fdff8 0x4fe232 0x51f8c9 0x51f704 0x51dbda 0x51da5c    0x51f647 0x14ec705 0x4202c0 0x420258 0x642ff4 0x14ec705 0x4202c0 0x420258 0x4e1021 0x4e157f    0x4e1056 0x646af9 0x14ec705 0x4202c0 0x420258 0x4e1021 0x4e157f 0x4e06e8 0x44fcef 0x44ff02   0x42dd4a 0x41f698 0x2500df9 0x2500ad0 0x1628bf5 0x1628962 0x1659bb6 0x1658f44 0x1658e1b 0x24ff7e3 0x24ff668 0x41cffc 0x22fd 0x2225)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

This is the output under the "Auto" pane :

argc = (int)1
argv = (char **)0xbffff3b4

SecondViewController.h :

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>



@interface SecondViewController : UIViewController 
<MKMapViewDelegate>


@property (strong, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)zoomIn:(id)sender;
- (IBAction)changeMapType:(id)sender;





@end

SecondViewController.m :

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (IBAction)zoomIn:(id)sender {
    MKUserLocation *userLocation = _mapView.userLocation;
    MKCoordinateRegion region =
    MKCoordinateRegionMakeWithDistance ( userLocation.location.coordinate, 50, 50);
    [_mapView setRegion:region animated:NO];
}

- (IBAction)changeMapType:(id)sender {
if (_mapView.mapType == MKMapTypeStandard)
    _mapView.mapType = MKMapTypeSatellite;
else
    _mapView.mapType = MKMapTypeStandard;
}

- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:
(MKUserLocation *)userLocation
{
    _mapView.centerCoordinate =
    userLocation.location.coordinate;
}

- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.showsUserLocation = YES;
_mapView.delegate = self; 



}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

I've followed a tutorial on the matter - so as you might have noticed, this is pretty greek to me. I'll be happy to supply additional information on the problem. Thanks in advance,

kind regards Yngve

EDIT: I created a new project and i recycled some of the code, for some reason it just worked. I believe the different answers and solutions gave me a heads up to pay more attention when working with programming. This is my first project, so i expect i might run into some more trouble, thank you all for taking the time to answer my question. (I actually find code exciting now)

  • Yngve
Nopes
  • 35
  • 8

3 Answers3

0

Looks like you just need to set the class of the second view controller to SecondViewController in the inspector pane of your storyboard. It's still set to a stock UIViewController.

geraldWilliam
  • 4,123
  • 1
  • 23
  • 35
  • I was just reading that on another forum - i tried and it still crashes with the same errors. – Nopes Aug 05 '13 at 20:29
  • Then you must have missed a step. The crash occurs because UIViewController knows nothing about the custom methods and properties that you have added to the UIViewController subclass you have created. With the view controller in question selected in Interface Builder, open the inspector pane, switch to the identity inspector tab, and in the Class field type SecondViewController. – geraldWilliam Aug 05 '13 at 20:44
  • It could also be that the nib owner was instantiated as `[ [ UIViewController alloc ] initWithNib:...` instead of `[ [ SecondViewController alloc ] initWithNib:...` – nielsbot Aug 05 '13 at 22:23
  • In any case, you have an instance of `UIViewController` (probably). It's definitely not an instance of `SecondViewController` – nielsbot Aug 05 '13 at 22:30
0

Sounds like the missing styleproto files might be the issue, see this SO answer: iOS6 Simulator MKMapKit "Couldn't find default.styleproto in framework"

Are you using a Storyboard or NIB? If so, you might want to see this SO answer and make sure you set the right custom class in IB: NSInvalidArgumentException isKindOfClass

Community
  • 1
  • 1
garrettmurray
  • 3,338
  • 1
  • 25
  • 23
0

For me mapView will sometimes throw exceptions in the simulator when you execute this line of code

_mapView.showsUserLocation = YES;

Restarting Xcode and clearing derived data is what fixes it for me when it gets in this state. Resetting simulator does nothing.

odyth
  • 4,324
  • 3
  • 37
  • 45