Using NSUserDefaults to store image co-ordinates - cocoa

I am moving images around in my app which is working fine. The problem is that I'd like to keep the images in place when the user comes back to that view. At present, I am using a string to hold the image centre:
NSString *centre = NSStringFromCGPoint(image.center);
I have an NSUserDefault variable called theCentre:
theCentre = [NSUserDefaults standardUserDefaults];
which I try to save using:
[theCentre setObject:centre forKey:#"centre"];
When I output the log, I get the following:
{280, 320}
which is great.
In the viewDidLoad, I have tried the exact same log and it is returning (null). I take it that this means it isn't saving theCentre properly. Am I missing something fundamental here because I have used NSUserDefaults elsewhere in my app and it has been working ok. Many thanks in advance.
Edit: this is the whole code to try and aid debugging :)
-(void)getBounds
{
lowerBoundX = 250;
upperBoundX = 310;
lowerBoundY = 290;
upperBoundY = 350;
}
#pragma mark -
#pragma mark UIPanGestureRecognizer selector
- (void) dragGesture:(UIPanGestureRecognizer *) panGesture
{
[self getBounds];
CGPoint translation = [panGesture translationInView:self.view];
switch (panGesture.state) {
case UIGestureRecognizerStateBegan:{
originalCentre = dragImage.center;
}
break;
case UIGestureRecognizerStateChanged:{
dragImage.center = CGPointMake(dragImage.center.x + translation.x,
dragImage.center.y + translation.y);
}
break;
case UIGestureRecognizerStateEnded:{
if (((dragImage.center.x >= lowerBoundX) && (dragImage.center.y >= lowerBoundY) &&
(dragImage.center.x <= upperBoundX) && (dragImage.center.y <= upperBoundY))) {
dragImage.center = CGPointMake(280, 320);
[dragImage setUserInteractionEnabled:NO];
theCentre = [NSUserDefaults standardUserDefaults];
NSString *centre = NSStringFromCGPoint(dragImage.center);
[theCentre setObject:centre forKey:#"centre"];
NSLog(#"%#", [theCentre objectForKey:#"centre"]);
[theCentre synchronize];
break;
}
[UIView animateWithDuration:0.5
animations:^{
dragImage.center = originalCentre;
}
completion:^(BOOL finished){
}];
}
break;
default:
break;
}
[panGesture setTranslation:CGPointZero inView:self.view];
}
-(IBAction)clear{
[UIImageView animateWithDuration:0.5 animations:^{
dragImage.center = CGPointMake(100, 200);
}];
[dragImage setUserInteractionEnabled:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"%#", [theCentre objectForKey:#"centre"]);
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(dragGesture:)];
[dragImage addGestureRecognizer:panGesture];
[dragImage setUserInteractionEnabled:YES];
}

Data saved in NSUserDefaults is first cached and thus it is not available instantly from other instances of the NSUserDefaults. The data is saved periodically on some predefined events/time intervals. However you can force a data synchronization by calling synchronize like this:
NSString *theCenter = NSStringFromCGPoint(image.center);
theCenter = [NSUserDefaults standardUserDefaults];
[theCenter setObject:centre forKey:#"center"];
[theCenter synchronize];

Related

NSView in cocoa refuses to redraw, whatever I try

I'm writing an application on mac to measure colors on screen. For that I have a ColorView that fills a bezierpath with a specific color. When measuring it goes well for most of the patches, but at a given moment the redraw staggers and the view does not get updated anymore. I have been looking for a while now, tried many proposed solutions. None of them are waterproof.
My actual code:
[colorView setColor:[colorsForMeasuring objectAtIndex:index]];
[colorView setNeedsDisplay:YES];
[colorView setNeedsLayout:YES];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue,^{
[colorView setNeedsDisplay:YES];
[colorView setNeedsLayout:YES];
[colorView updateLayer];
[colorView displayIfNeeded];
[colorView display];
});
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[NSApp runModalSession:aSession];
The drawcode of my colorView is as follows:
- (void)display
{
CALayer *layer = self.layer;
[layer setNeedsDisplay];
[layer displayIfNeeded];
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
[self internalDrawWithRect:self.bounds];
}
- (void)internalDrawWithRect:(CGRect)rect
{
NSRect bounds = [self bounds];
[color set];
[NSBezierPath fillRect:bounds];
}
- (void)drawRect:(CGRect)rect {
[self internalDrawWithRect:rect];
}
Any help would be very welcome! My original code was much simpler, but I kept on adding things that might have helped.
As I said, the original code was rather straightfoward and simple:
The measuring loop:
for ( uint32 index = 0; index < numberOfColors && !stopMeasuring; index++ )
{
#ifndef NDEBUG
NSLog( #"Color on screen:%#", [colorsForMeasuring objectAtIndex:index] );
#endif
[colorView setColor:[colorsForMeasuring objectAtIndex:index]];
[colorView setNeedsDisplay:YES];
[colorView displayIfNeeded];
// Measure the displayed color in XYZ values
CGFloat myRed, myGreen, myBlue, myAlpha;
[[colorsForMeasuring objectAtIndex:index] getRed:&myRed green:&myGreen blue:&myBlue alpha:&myAlpha];
float theR = (float) myRed;
float theG = (float) myGreen;
float theB = (float) myBlue;
xyzData = [measDeviceController measureOnceXYZforRed:255.0f*theR Green:255.0f*theG Blue:255.0f*theB];
if ( [xyzData count] > 0 )
{
measuringResults[index*3] = [[xyzData objectAtIndex:0] floatValue];
measuringResults[(index*3) + 1] = [[xyzData objectAtIndex:1] floatValue];
measuringResults[(index*3) + 2] = [[xyzData objectAtIndex:2] floatValue];
#ifndef NDEBUG
printf("Measured value X=%f, Y=%f, Z=%f\n", measuringResults[index*3], measuringResults[(index*3) + 1], measuringResults[(index*3) + 2]);
#endif
} }
The drawing of the MCTD_ColorView which just is inherited from NSView:
- (void)setColor:(NSColor *)aColor;
{
[aColor retain];
[color release];
color = aColor;
}
- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[color set];
[NSBezierPath fillRect:bounds];
}
- (void)dealloc
{
[color release];
[super dealloc];
}
When running my measurement loop and after putting breakpoints in "setColor" and "drawRect", debugging always stops in setColor but never in drawRect. Without debugging, the view remains white while I should see all different colors appearing.
As can be seen in my first post, I have tried many things to get it drawing, but they all failed.

MapView Problems - Pin change color when map reloded

I have a problem with the PIn color mapView when a refresh is done.
In my I app i display some point with two color in order to identify if a service is available.
On the first start, no problems appear. The code is the follower:
- (void)viewDidLoad
{
[super viewDidLoad];
[self dowloadPoint]; // here I exucte the first start
}
- (void)dowloadPoint{
NSURL *url1 =[NSURL URLWithString:#"http:MYUSRL"];
NSData *datos1 =[[NSData alloc] initWithContentsOfURL:url1];
[self plotBarPosition:datos_string1]; //Here I call the plotBarPosition method
}
- (void)plotBarPosition:(NSString *)datos_string1 {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:#"features"];
// Get the objects you want, e.g. output the second item's client id
NSArray *items_properties = [json valueForKeyPath:#"properties"];
NSArray *items_geo = [json valueForKeyPath:#"geometry"];
for (int i = 0; i < [json count]; i++){
NSString *nomprePunto =[[items_properties objectAtIndex:i] objectForKey:#"title"];
NSNumber *lat =[[[items_geo objectAtIndex:i] objectForKey:#"coordinates"] objectAtIndex:0];
NSNumber *lon =[[[items_geo objectAtIndex:i] objectForKey:#"coordinates"] objectAtIndex:1];
CLLocationCoordinate2D coordinate;
coordinate.latitude = lat.doubleValue;
coordinate.longitude = lon.doubleValue;
//ESTADO
NSString *description = [[items_properties objectAtIndex:i] objectForKey:#"description"];
NSString *estado_punto = [[NSString alloc]init];
if ([description rangeOfString:#"Averiado"].location == NSNotFound) {
estado_punto = #"Available";
} else {
estado_punto = #"NOt Available";
averiados ++;
}
NSString *averiadosStr = [NSString stringWithFormat:#"%d",averiados];
averiadosLabel.text = averiadosStr;
MyLocation *location =[[MyLocation alloc] initWithName:nomprePunto coordinate:coordinate estado:estado_punto];
[_mapView addAnnotation:location];
}
}
- (MKPinAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyLocation *)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
if([[annotation estado] isEqualToString:#"En Servicio"])
annotationView.pinColor = MKPinAnnotationColorGreen;
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
But whe I add a refres button that is function is simply a refreshcalling the dowloadPoint once again,
- (IBAction)refresh{
[self dowloadPoint];
}
the color of pins change in a "random manner", not corrisponding with the real state of point.
Any ideas about what is happening? Thanks in advance.
EDIT: It seemps pproblems is due to:
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
erasing it, the app work properly but pins area drown abow the previous ones...:S
The default color of the pin is red. You set it to green if the estado property of your MyLocation object is equal to #"En Servicio". I understand that sometimes the color is red, when your estado property is equal to #"En Servicio", or sometimes green when it is not.
One reason could be that your MyLocation object simply does no longer exist when you press the refresh button. In this case, you might still have a pointer to the memory location where it once existed, but this location may have been overwritten by anything, causing a random color.
This can happen e.g. if your MyLocation object has been created as an autorelease object that has been released when you returned to the main event loop, i.e. to handle user interactions.
This should not be the case if you are using ARC.

how to display the value of lable using tag in xcode?

I have auto generated some Labels and now I need to overwrite the value in the label by getting the value of other label using its tag in xcode.
This is my onclick code:
- (IBAction)onClick1:(id)sender
{
NSString *title = [(UIButton *)sender currentTitle];
NSLog(#"title ~~~~~~~~~~~ %#",title);
for(i=0;i<vCount;i++) {
pStr = [NSString stringWithFormat:#"%c",[pStrName characterAtIndex:i]];
[pLblMyLable1 setTag:j+1];
iTag = [pLblMyLable1 tag];
NSLog(#"ssssstttttrrrrriinnnngggg on click %#", pStr);
if([title isEqualToString:pStr]){
//pStr = [NSString stringWithFormat:#"%c",[pStrName characterAtIndex:i]];
NSLog(#"came into if");
pLblMyLable1.text = pStr;
//pLblMyLable1 = [self.view viewWithTag:iTag];
// pStr = [pLblMyLable1.text:iTag];
/*pLblMyLable1 = [self.view viewWithTag:1];
pLblMyLable1 = [self.view viewWithTag:2];
pLblMyLable1 = [self.view viewWithTag:3];
pLblMyLable1 = [self.view viewWithTag:4];
pLblMyLable1 = [self.view viewWithTag:5];
pLblMyLable1 = [self.view viewWithTag:6];*/
}
You need to enumerate through all the subviews grabbing only the UILabels. This can be done like this:
for(UIView *subview in self.view.subviews){
    if([subview isKindOfClass:[UILabel class]]){
NSLog(#"%d", [(UILabel*)subview tag]);
}
}
This should get you going in the right direction.
Edit
In response to comment:
You can try this to see if it gets rid of error.
for(UIView *subview in [self.view subviews]){
if([subview isKindOfClass:[UILabel class]]){
NSLog(#"%d", [subview tag]);
}
}
this was the thing i did since i used a single label.... now works fine, intrestingly works good
NSString *title = [(UIButton *)sender currentTitle];
for(i=0;i<vCount;i++) {
for (NSString* key in Name){
UILabel *Select = (UILabel*)[self.view viewWithTag:[key intValue]];
if ([title isEqualToString:Select.text]) {
Str = [NSString stringWithFormat:#"%c",[pStrName characterAtIndex:i]];
}}

Page curl up - for landscape left and landscape right

I am trying to create a page turn application in xcode. The page turn works fine for landscape right but for landscape left, the page curl happens from the left. I added a container view and then the page curl on landscape left started happening from the bottom but not from right.
I found some suggestions here (http://stackoverflow.com/questions/4780488/i-want-to-use-animation-to-imageview-using-uianimationtransitioncurl-right-or-lef) and here (http://stackoverflow.com/questions/4780488/i-want-to-use-animation-to-imageview-using-uianimationtransitioncurl-right-or-lef)
but these both suggestions were with old style of page transition code and not with the block code. I tried to modify these with new block style code but it is not working for me.
I am attaching my code here:
- (void)viewDidLoad
{
UIImage *img = [UIImage imageNamed:#"s1p1.png"];
currentView.image = img;
img = [UIImage imageNamed:#"s1p2.png"];
swapView.image = img;
UISwipeGestureRecognizer *nextPage;
nextPage = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(turnPage)] autorelease];
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft) {
[nextPage setDirection:UISwipeGestureRecognizerDirectionRight];
}
else {
[nextPage setDirection:UISwipeGestureRecognizerDirectionLeft];
}
[self.view addGestureRecognizer:nextPage];
[super viewDidLoad];
}
-(void) turnPage
{
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)
{
NSLog(#"landscapeleft");
mainView.transform = CGAffineTransformMakeRotation(M_PI);
currentView.transform = CGAffineTransformMakeRotation(-M_PI);
swapView.transform = CGAffineTransformMakeRotation(-M_PI);
[UIView transitionWithView:mainView
duration:1
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
currentView.hidden = YES;
swapView.hidden = NO;
}
completion:^(BOOL finished){
UIImage *temp = [currentView.image retain];
currentView.image = swapView.image;
swapView.image = temp;
[temp release];
currentView.hidden = NO;
swapView.hidden = YES;
}];
}
else
{
NSLog(#"landscaperight");
[UIView transitionWithView:self.view
duration:1
options:UIViewAnimationOptionTransitionCurlUp
animations:^{
currentView.hidden = YES;
swapView.hidden = NO;
}
completion:^(BOOL finished){
UIImage *temp = [currentView.image retain];
currentView.image = swapView.image;
swapView.image = temp;
[temp release];
currentView.hidden = NO;
swapView.hidden = YES;
}];
}
}
where I have two views: the self.view is the primary view from the window and then the mainView is my container view. currentView and SwapView are imageviews.
Any thoughts on this? Many thanks for reviewing this.

IPad--how to refresh subview on ipad?

I add 4 subviews(UIScrollView,UITableView)in main view on ipad, and each view has it own data.
In Marco view, I try to reload data from server every 3 second to refresh my Marco View.
as first pic..
I set 3 Marco icons on ipad main view, when I delete one on popoverView.
Icons on popoverView really delete, but still show up 3 icons on main view.
as second and third pic...
It must be delete from background and then open app it gonna be correct view..
as fourth pic..
Dose any one know what's problem.. I try to use [marco_scrollview setNeedsDisplay] .
but it no work..
Thank in advance..
here is my code to get Marcodata:
- (void)getTheMacroURLData{
//[self.view setNeedsDisplay];
// [marco_scrollview setNeedsDisplay];
self.macroList = nil;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
EnvoTouchAppDelegate *appDelegate = (EnvoTouchAppDelegate *)[[UIApplication sharedApplication] delegate];
struct sockaddr_in Addr;
bzero(&Addr, sizeof(Addr));
Addr.sin_len = sizeof(Addr);
Addr.sin_family = AF_INET;
SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &Addr);
SCNetworkReachabilityFlags flags;
SCNetworkReachabilityGetFlags(target, &flags);
if (flags & kSCNetworkReachabilityFlagsIsWWAN){
//Serial Number not save in the keychain now
NSString *theNewURLString = [NSString stringWithFormat:eGetAllMacrosSettingURL3G,[userDefaults objectForKey:#"ecsn"],appDelegate.udid];
NSURL *theNewURL = [NSURL URLWithString:theNewURLString];
self.macroList = [NSMutableArray arrayWithContentsOfURL:theNewURL];
}
if (flags & kSCNetworkFlagsReachable){
NSString *theNewURLString = [NSString stringWithFormat:eGetAllMacrosSettingURLWiFi,appDelegate.ipString,appDelegate.udid];
NSURL *theNewURL = [NSURL URLWithString:theNewURLString];
self.macroList = [NSMutableArray arrayWithContentsOfURL:theNewURL];
}
int k=0;
for (int i=0; i<[self.macroList count]; i++) {
int j=i%9;
if (j==0) {
k++;
}
UIButton *iconButton=[UIButton buttonWithType:UIButtonTypeCustom];
NSString *picString = [[self.macroList objectAtIndex:i] valueForKey: #"pid"];
UIImage *buttonImage = [UIImage imageNamed:picString];
[iconButton setImage:buttonImage forState:UIControlStateNormal];
[iconButton setTag:i];
[iconButton addTarget:self action:#selector(exeMacro:) forControlEvents:UIControlEventTouchUpInside];
[iconButton setFrame:CGRectMake(30+(i%9*80), 17+((k-1)*70), 48, 48)];
//[self.view addSubview:iconButton];
[marco_scrollview addSubview:iconButton];
if ([self.macroList count] > 18) {
[marco_scrollview setContentSize:CGSizeMake(320, 525 + (([self.macroList count]/4 - 4) * 70) )];
}
}
[marco_view addSubview:marco_scrollview];
}
And I try to reload data every 3 second..
I dont know why it doesn't refresh Marco View..
The display makes it appear as if you are using a tableview somewhere. If so, are you calling the reloadData method on the table view?

Resources