How to call a variable from another class - ios

First off, do you guys know of any good tutorial sites or books to learn how to code for iOS6?
Now to the problem at hand, I have an AppDelegate class and a ViewController class.
I am using the AppDelegate class to find my current location and save it as an NSString.
I have a label in my ViewController class where I want to display my location.
In java I would usually just do something like this
label.text = AppDelegate.myLocationString;
But I am fairly new to objective c and I don't know how to do the equivalent. To be honest I'm not even sure if I am taking the correct approach for this language. Is there another way to approach this problem in objective c?
I've tried searching around for how to do it but I'm not even sure that I'm phrasing my searches correctly so I don't know if I'm searching in the right direction.
Any help that could be offered would be very much appreciated
My code as it is now:
AppDelegate.h
#import <UIKit/UIKit.h>
#import CoreLocation/CoreLocation.h
#class BIDViewController;
#interface BIDAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) BIDViewController *viewController;
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) NSString *myPosition;
#end
LocationManager class from AppDelegate.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15) {
//if it is within the last 15 seconds, use it. Else do nothing.
if (newLocation.horizontalAccuracy < 35.0) {
//location seems pretty accurate so we will use it
NSLog(#"latitude: %+.6f, longitude: %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(#"Horizontal accuracy: %f", newLocation.horizontalAccuracy);
int latDegrees = newLocation.coordinate.latitude;
int lonDegrees = newLocation.coordinate.longitude;
double latDecimal = fabs(newLocation.coordinate.latitude - latDegrees);
double lonDecimal = fabs(newLocation.coordinate.longitude - lonDegrees);
int latMinutes = latDecimal * 60;
int lonMinutes = lonDecimal * 60;
double latSeconds = ((latDecimal * 3600) - (latMinutes * 60));
double lonSeconds = ((lonDecimal * 3600) - (lonMinutes * 60));
_myPosition = [NSString stringWithFormat:#"Lat: %d, %d, %1.4f, Lon: %d, %d, %1.4f", latDegrees, latMinutes, latSeconds, lonDegrees, lonMinutes, lonSeconds];
}
}
}
ViewController.h
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *positionLabel;
#end
ViewController.m
#import "BIDViewController.h"
#import "BIDAppDelegate.h"
#interface BIDViewController ()
#end
#implementation BIDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//I have tried setting the label's text here but to no avail so far, I have been
//able to set it as "blah" with the following line:
//
// positionLabel.text = #"blah";
//
//and I have tried things such as the following to populate the label with the
//myPosition variable:
//
// positionLabel.text = _myPosition;
// or
// positionLabel.text = AppDelegate._myPosition;
}
- (void)viewDidUnload
{
[self setPositionLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end

To reference the App Delegate from your view controller, first import its definition (which you've ready done):
#import "BIDAppDelegate.h"
and then in code get the delegate:
BIDAppDelegate *appDelegate = (BIDAppDelegate *)[[UIApplication sharedApplication] delegate];
and then get the location from the delegate:
CLLocation *location = appDelegate.locationManager.location;
and then call whatever method you like on the location object:
NSString *descr = location.description;
Or get the pre-calculated position with:
NSString *position = appDelegate.myPosition;

In your ViewController
BIDAppDelegate *appDelegate = (BIDAppDelegate *)[UIApplication sharedApplication].delegate;
[positionLabel setText:appDelegate.myPosition];

How to call a variable from another class?
Assume you are in ClassB
ClassA *aObj=... ; //create an object of ClassA
aObj.propertyName=...;//set the property or ivar
NSString *string=aObj.propertyName;//retrieve it
do you guys know of any good tutorial sites or books to learn how to
code for iOS6?
http://www.lynda.com/Apple-training-tutorials/106-0.html
http://www.raywenderlich.com/tutorials

Related

Measuring the distance between latest coordinates with a fixed one in iOS showing larger distance while they are very near

I am getting inaccurate result while measuring distance between latest coordinate and some a fixed coordinate which can be done by taping on the "Set fixed location" button.
Initially the distance is 0.0 meter as no location is fixed which is the first screenshoot below. Once I tap on the "Set fix location" button, it sets the the coordinate where I am standing at the time of pressing the button and the distance is immediately started calculated which is in the second screenshoot.
When I walk around 10 meters or more and come back to the position where I took the initial fixed location coordinate, its showing me more than 7 meters distance, although it should be less than half meter. I am testing this demo app outside in an open ground.
I have pasted all my code below and could you please help me to fixed the issue? I am new in GPS location related world and please kindly let me know if I am missing anything in the code. Are there any other ways to achieve more accurate result? I have been checking many stack-overflow articles and none help me to improve in my case.
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLLocation *fixedLocation;
#property (strong, nonatomic) CLLocation *latestLocation;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *fixedLocationLatitude;
#property (weak, nonatomic) IBOutlet UILabel *fixedLocationLongitude;
#property (weak, nonatomic) IBOutlet UILabel *latestLocationLatitude;
#property (weak, nonatomic) IBOutlet UILabel *latestLocationLongitude;
#property (weak, nonatomic) IBOutlet UILabel *distanceBetweenLocations;
- (IBAction)setFixLocation:(UIButton *)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.fixedLocation = [[CLLocation alloc] initWithLatitude:90.0 longitude:0.0]; // North pole if no location is fixed for the first time
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
if(locations.lastObject!=nil){
self.latestLocation = locations.lastObject;
self.latestLocationLatitude.text = [NSString stringWithFormat:#"%.8f", self.latestLocation.coordinate.latitude];
self.latestLocationLongitude.text = [NSString stringWithFormat:#"%.8f", self.latestLocation.coordinate.longitude];
}
if(self.fixedLocation != nil && self.latestLocation != nil){
CLLocationDistance distanceInMeter = [self.latestLocation distanceFromLocation:self.fixedLocation];
self.distanceBetweenLocations.text = [NSString stringWithFormat:#"%.2f",distanceInMeter] ;
} else {
self.distanceBetweenLocations.text = [NSString stringWithFormat:#"%.f",0.0] ;
}
}
- (IBAction)setFixLocation:(UIButton *)sender {
if(self.locationManager.location != nil){
self.fixedLocation = self.locationManager.location;
self.fixedLocationLatitude.text = [NSString stringWithFormat:#"%.8f", self.fixedLocation.coordinate.latitude];
self.fixedLocationLongitude.text = [NSString stringWithFormat:#"%.8f", self.fixedLocation.coordinate.longitude];
}
}
#end

IOS CLLocation manager returning zero speed

Hi I want to calculate speed within my camera view in XCODE8. The code does not generate error but my speed is returning zero. So I searched the link below :Self Location Manager Delegate error in Xcode 6. My code has two interface files, one for CvVideoCamera and one for ClLocationManager. I never had two interface files in my project, so I am not sure I am doing everything correctly. My code is below, Can you please tell me if something is wrong in my code architecture that causing me to get zero speed.
viewcontroller.h
#interface ViewController : UIViewController<CvVideoCameraDelegate>
#property (nonatomic, strong) CvVideoCamera* videoCamera;
…
#end
#interface LocationController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager*locationManager;
#end
viewcontroller.m
#import "ViewController.h"
int speed;
#interface LocationController() {
}
#end
#implementation LocationController
#synthesize locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = (id)self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = locations.lastObject;
loc = locations.lastObject;
speed = loc.speed * 2.236;
}
#end
#interface ViewController (){
….
}
#end
#implementation ViewController
#synthesize imageView;
#synthesize videoCamera
- (void)viewDidLoad
{
[super viewDidLoad];
self.videoCamera.delegate = self;
(void)processImage:(cv::Mat&)image {
NSLog(#"speed: %.d”, speed)}

Pass an array from ViewController to AppDelegate

I have an array of floating point values called playbackRates, set in a ViewController. I want to use these values in a function within my appdelegate. How would I access these values from within my appdelegate?
ViewController.h
#interface P15ViewController : UIViewController <GuitarStringsViewDelegate>
{
float playbackRates[6];
}
ViewController.m
- (void)viewDidLoad
{
self.guitarStringView.delegate = self;
chordNameLabel.text = chordName;
self.guitarStringView.chordName=chordName;
if([chordName isEqualToString:#"A"]){
playbackRates[0] = 1.0;
playbackRates[1] = 1.0;
playbackRates[2] = 2 * pow(2, (2/12));
playbackRates[3] = 2 * pow(2, (2/12));
playbackRates[4] = 2 * pow(2, (2/12));
playbackRates[5] = 1.0;
}
else if([chordName isEqualToString:#"B"]){
//set rates
}
else if([chordName isEqualToString:#"C"]){
//set rates
}
[super viewDidLoad];
}
appdelegate.h
#import <UIKit/UIKit.h>
#import <PAEEngine/PAEEngine.h>
#interface P15AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
-(void)play:(int)index velocity:(float)velocity;
#end
appdelegate.m - player.rate needs to be set by each value in the array within the for loop in the play method
#import "P15AppDelegate.h"
#import "P15ViewController.h"
#interface P15AppDelegate ()
#property (nonatomic, strong) PAEAudioHost* host;
#property (nonatomic, strong) NSArray* channelStrips;
#property (nonatomic, strong) NSArray* filemnames;
#property (nonatomic) int nextVoice;
#end
#implementation P15AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.host = [PAEAudioHost audioHostWithNumOutputs:2];
self.filemnames = #[#"guitar-E1.wav", #"guitar-A2.wav", #"guitar-D3.wav",
#"guitar-G3.wav", #"guitar-B3.wav", #"guitar-E4.wav"];
const int numVoices = 8;
NSMutableArray* channelStrips = [[NSMutableArray alloc] initWithCapacity:numVoices];
for (int i = 0; i < numVoices; ++i)
{
PAEChannelStrip* channelStrip = [PAEChannelStrip channelStripWithNumChannels:2];
[channelStrips addObject:channelStrip];
}
self.channelStrips = [NSArray arrayWithArray:channelStrips];
PAEAmplitude* amp = [PAEAmplitude amplitudeWithNumInputs:2];
amp.input = [PAEMixer mixerWithSources:self.channelStrips];
amp.level.input = [PAEConstant constantWithValue:1.0 / numVoices];
self.host.mainMix = amp;
[self.host start];
return YES;
}
-(void)play:(int)index velocity:(float)velocity;
{
if (index >= 0 && index < self.filemnames.count)
{
PAEAudioFilePlayer* player = [PAEAudioFilePlayer audioFilePlayerNamed:self.filemnames[index]];
player.loop = NO;
player.rate - playbackRates[i]; --> **NEED TO ACCESS ARRAY HERE**
PAEAmplitude* amp = [PAEAmplitude amplitudeWithNumInputs:player.numChannels];
amp.input = player;
amp.level.input = [PAEConstant constantWithValue:velocity];
PAEChannelStrip* channelStrip = self.channelStrips[self.nextVoice];
channelStrip.input = amp;
self.nextVoice++;
if (self.nextVoice == self.channelStrips.count)
self.nextVoice = 0;
}
}
#end
First you'll need to write a public accessor for the playbackRates array in your UIViewController subclass named P15ViewController.
P15ViewController.h add -
- (float)playbackRateAtIndex:(NSInteger)index;
P15ViewController.m add -
- (float)playbackRateAtIndex:(NSInteger)index {
return playbackRates[index];
}
Your appDelegate needs to know of this method so import the P15ViewController.h in your P15AppDelegate.m, which I see you've already done.
Then assuming the viewController is the rootViewController of the appDelegate then you can access it with the following code.
Replace -
player.rate - playbackRates[i]; --> **NEED TO ACCESS ARRAY HERE**
With -
player.rate = [(P15ViewController*)self.window.rootViewController playbackRateAtIndex:i];
The typecast is needed so as to enable the calling of the accessor (more specifically a getter).
If a more direct access way is needed then change the getter or add another getter to return the typed address of the playbackRates array like this in the P15ViewController.m -
- (float*)playbackRatesArray {
return &playbackRates;
}
Remember to update the header file also!
And access it like this from P15AppDelegate.m, since a C array is just a pointer -
float* playbackRates = [(P15ViewController*)self.window.rootViewController playbackRatesArray];
player.rate = playbackRates[i];

objective-c locationManager as delegate, be notified if location changed

I sourced my locationManager out in a own class and file
Now I want to be notified when the location is updated. So I tried to implement the delegation pattern. But for some reason it does not work. What I did:
In Location.h: specified class, protocol, ivar and property id, delegation method
In Location.m: added a call to the delegation method
In ViewController.h: added delegation protocol
In ViewController.m: implemented the delegation method
Build and run the code. But the delegation method in ViewController.m is not called :(
Any ideas what i missed?
Output
2013-07-04 11:55:30.429 Sandbox2[2001:c07] Inside Lokation::getLocation
2013-07-04 11:55:30.443 Sandbox2[2001:c07] Inside Lokation::locationManager
2013-07-04 11:55:30.449 Sandbox2[2001:c07] currentLat: 51.509980 currentLng -0.133700
// Missing NSLog Statement of the delegate method didFindLocation...
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
// added Lokation header
#import "Lokation.h"
// added LokationDelegate
#interface ViewController : UIViewController <CLLocationManagerDelegate, LokationDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) Lokation *lokation;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[self getLocation];
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
// added delegate function of Lokation
// Should be run after the locationmanager found a location
-(void)didFindLocation {
NSLog(#"I am found a lokation and I am now in the ViewController");
}
Lokation.h (20130704 updated to working code)
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
// Added class, protocol and method for the delegate
#class Lokation;
#protocol LokationDelegate <NSObject>
-(void)didFindLocation;
#end
#interface Lokation : NSObject <CLLocationManagerDelegate> {
CLLocationDegrees currentLat;
CLLocationDegrees currentLng;
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *currentLoc;
// added delegate property
#property (assign, nonatomic) id<LokationDelegate> delegate;
-(void)getLocation;
#end
Lokation.m (20130704 updated to working code)
#import "Lokation.h"
#implementation Lokation
#synthesize locationManager = _locationManager;
#synthesize currentLoc = _currentLoc;
-(void)getLocation {
NSLog(#"Inside Lokation::getLocation");
// active location determination
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// We don't want to be notified of small changes in location,
// preferring to use our last cached results, if any.
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Inside Lokation::locationManager");
if (!oldLocation ||
(oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {
currentLat = newLocation.coordinate.latitude;
currentLng = newLocation.coordinate.longitude;
} else { // oldLocation
currentLat = oldLocation.coordinate.latitude;
currentLng = oldLocation.coordinate.longitude;
}
self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];
NSLog(#"currentLat: %f currentLng %f", currentLat, currentLng);
// added call to the delegate function
[self.delegate performSelector:#selector(didFindLocation)];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(#"%#", error);
}
Hmm..i think you are doing right but if i do change like below:
remove __unsafe_unretained id<LokationDelegate> delegate; and replace #property (assign, nonatomic) id<LokationDelegate> delegate with #property (retain) id<LokationDelegate> delegate (and synthesize property),it works like charm for me

iOS Route-Me: Add User Location Marker

I have been trying to add the following things to a Route-Me app.
Add a marker at the starting location
Move the map to the user location
Move the marker to that new location
I am using the basic example from MapBox ios example, as my maps are from an offline mbtiles store.
This is my header file:
#import <UIKit/UIKit.h>
#import "RMMapView.h"
#import "RMMarker.h"
#import "RMMapViewDelegate.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#interface MBTiles_ExampleViewController : UIViewController
{
RMMapView *mapView;
}
#property (nonatomic, retain) IBOutlet RMMapView *mapView;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) CLLocation *currentLocation;
#property (nonatomic, retain) RMMarkerManager *markerManager;
#property (nonatomic, retain) RMMarker *locationMarker;
#end
And this is my implementation file:
#define kStartingLat 30.0f
#define kStartingLon -10.0f
#define kStartingZoom 1.5f
#import "MBTiles_ExampleViewController.h"
#import "RMMBTilesTileSource.h"
#import "RMMapContents.h"
#import "RMMarker.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#implementation MBTiles_ExampleViewController
#synthesize mapView;
#synthesize currentLocation;
#synthesize locationManager;
#synthesize markerManager;
#synthesize locationMarker;
(void)viewDidLoad
{
CLLocationCoordinate2D startingPoint;
startingPoint.latitude = kStartingLat;
startingPoint.longitude = kStartingLon;
NSURL *tilesURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"control-room-0.2.0" ofType:#"mbtiles"]];
RMMBTilesTileSource *source = [[[RMMBTilesTileSource alloc] initWithTileSetURL:tilesURL] autorelease];
[[[RMMapContents alloc] initWithView:self.mapView
tilesource:source
centerLatLon:startingPoint
zoomLevel:kStartingZoom
maxZoomLevel:[source maxZoom]
minZoomLevel:[source minZoom]
backgroundImage:nil] autorelease];
mapView.enableRotate = NO;
mapView.deceleration = NO;
mapView.backgroundColor = [UIColor blackColor];
mapView.contents.zoom = kStartingZoom;
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
UIImage *iconImage = [UIImage imageNamed:#"marker.png"];
locationMarker = [[RMMarker alloc] initWithUIImage: iconImage];
[markerManager addMarker: locationMarker AtLatLong: startingPoint];
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[mapView moveToLatLong:newLocation.coordinate];
RMLatLong newCoords = {newLocation.coordinate.latitude, newLocation.coordinate.longitude};
if (nil != markerManager)
[markerManager moveMarker:locationMarker AtLatLon: newCoords];
}
(void)dealloc
{
[mapView release];
[super dealloc];
}
#end
The marker.png has been added to my resources folder.
So my questions
Why is my starting marker not showing?
I am using xcode on SnowLeopard, so can the simulator actually find my location? As the map does not move.
Any help would be great as I have tried so many code snippets and tutorials but none have ended up working.
Regarding your second question, from the apple docs:
In Xcode 4.0 and 4.1, you could simulate only the current location in your application. As of Xcode 4.2, you can simulate locations other than your current location in iOS applications that use Core Location. To set a location, choose Edit Scheme from the scheme selector in the toolbar, select the Run action, and click the Options tab. You can then choose a location from the Location menu
http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html
I was able to get it working after cleaning marker after move.
marker = [[RMMarker alloc] initWithUIImage:[UIImage imageNamed:#"marker.png"] anchorPoint:CGPointMake(0.5f, 1.f)];
[mapView.contents.markerManager addMarker:marker AtLatLong:locPoland];
[mapView.contents.markerManager moveMarker:marker AtLatLon:locWawa];
[mapView.markerManager moveMarker:marker AtLatLon: locUser];
[mapView moveToLatLong:locUser];
marker = nil;

Resources