Monday 16 September 2013

Difference between Frame and Bounds in iphone programming

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).
The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
So, imagine a view that has a size of 100×100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view’s bounds and frame:
// This method is in the view controller of the superview
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);
}
And the output of this code is:
bounds.origin.x: 0
bounds.origin.y: 0
bounds.size.width: 100
bounds.size.height: 100

frame.origin.x: 25
frame.origin.y: 25
frame.size.width: 100
frame.size.height: 100
So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).


referred from stackOverflow

Saturday 22 December 2012

Displaying Time as an integers in objective c


 NSDate *today = [NSDate date];
 NSCalendar *gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
 NSDateComponents *components =
[gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
    
    NSInteger hour = [components hour];
    NSInteger minute = [components minute];
    NSInteger second = [components second];

Wednesday 26 September 2012

JSON Parsing in iphone


Parsing the JSON objects in iphone using the default framework which was provided in ios5.
this framework is very simple to implement.
The response from the sample json is the string, arrays or Json objects.

The typical thing is the code changes based on the type of out put we get with the json.
The json response may be an array or string or a dictionary. so based on that we should perform the coding.

the following code replicates the json parsing.
   
//write this part of code in viewdidload:
 // h ah i have taken the url from the android tutorial
NSString *url=[NSString stringWithFormat:@"http://api.androidhive.info/contacts/"];
//getting the contents of url into data    
  NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    //using nsjsonserialisation
//the below part of code is used to take data fron NSData object and converts into string,
nsarray,NSDictionary based on options  NSJSONReadingMutableContainers then it returns the NSArray or NSDictionary
//if the options NSJSONReadingMutableContainers then it returns nsarray or dictioanry or if 
    NSDictionary *jsonse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"json response %@",jsonse);

 NSArray *subarr=[jsonse objectForKey:@"contacts"];
    NSLog(@"subarray %@",subarr);
    
    for (NSDictionary *d in subarr) {
        [names addObject:[d objectForKey:@"phone"]];  
        
        NSLog(@"names %@",names);
        for (NSDictionary *numbers in names) {
            NSLog(@"%@",[numbers objectForKey:@"mobile"]);
        }    }
    

Tuesday 25 September 2012

NSUser Defaults


NSUserdefault

hii NSUserDefault is the default class provided to store some information by default. The storage is permanent storage  but stores a very little content as a dictionary. It uses a key value pairing concept.

the sample code to store the data in NSUserdefaults is

-(IBAction)savetouserdefaults:(id)sender
{
    
    
    
    NSUserDefaults *defaultsstorage=[NSUserDefaults standardUserDefaults];
//text1.text is the my textfiled1 data    
    [defaultsstorage setObject:text1.text forKey:@"name"];
    [defaultsstorage setObject:text2.text forKey:@"lastname"];
    
    [defaultsstorage synchronize];
    

}


// to retrieve the data from nsuserdegfaults


prints the associated data with the keys
 NSUserDefaults *defauls=[NSUserDefaults standardUserDefaults];
    
    NSLog(@”%@”,[defauls objectForKey:@"name"]);
    NSLog(@” lastname %@”,[defauls objectForKey:@"lastname"]);


Monday 19 December 2011

Error "Illegal Configuration - Pattern colors on iOS versions prior to 3.0"


WEDNESDAY, 26 OCTOBER 2011

Error "Illegal Configuration - Pattern colors on iOS versions prior to 3.0"

While looking at some old projects developed with Cocos2D from the net, found that the new Xcode 4.2 throws the following error: "Illegal Configuration - Pattern colors on iOS versions prior to 3.0" and refused to compile.


Checked the net and found this page from Mark Tomlinson's site which says "The error is due to incompatibilities with older nib/xib files prior to version 3.0". It also mentioned about the fix is to "change the **Deployment** type in the *Interface Builder Document* settings", however the image no longer exist so I got no idea what he is talking about.

It took me a while and I finally found it. You have to select the nib/xib file, then at the right hand side, as shown below, under "Document Versioning" there's this drop down called "Deployment" and the current selected option is "iOS 2.0", just change it to the current version "iOS 5.0" (or anything above 3.0) then it works.