Monday, April 18, 2011

jSon Parse

First Import the ASIHttpRequest Classes into our project.

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:location_cookie_url];
        [request setDelegate:self];
        request.shouldRedirect = NO;
        [request startAsynchronous];   

- (void)requestFinished:(ASIHTTPRequest*)request
{
        if ([[[request responseHeaders] objectForKey:@"Set-Cookie"] hasPrefix:@"Preferences"])
        {

            ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:current_client_url];
            NSString*Strr = [NSString stringWithFormat:@"{\"IsMobile\": true, \"offerGuid\": \"%@\"}",[self.mProduct objectForKey:@"Guid"]];
           
            NSLog(@"%@",Strr);
           
            [request appendPostData:[Strr dataUsingEncoding:NSUTF8StringEncoding]];
            [request setDelegate:self];
            [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
            [request startAsynchronous];
        }
 }

- (void)requestFailed:(ASIHTTPRequest*)request
{
}

Thursday, January 20, 2011

How to reduce the distance between sections in UITableview

Use all these methods to reduce the height between sections in tableview

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
      return 3.0;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 3.0;
}

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}



Application should run on background or not

  1. Open your info.plist file
  2. Add The Key UIApplicationExitsOnSuspend or Select Application does not run in background
  3. Set the new key to YES or Fill in the tick box

Wednesday, January 19, 2011

How to disable the sleep mode

    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Monday, January 17, 2011

Digital Font for the Label

[myLabel setFont:[UIFont fontWithName:@"DBLCDTempBlack" size:42.0]];

Friday, January 14, 2011

Generate Random Number in a given range

TestViewController.h

#define RANDOM_SEED() srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF))
#define RANDOM_NUM() (random())


@interface TestViewController : UIViewController
...

TestViewController.m



#include <mach/mach_time.h>
@implementation TestViewController
-(int)getRandomNumber
{
RANDOM_SEED();
int randomSelect=RANDOM_NUM()%([yourArray count]-1); // picks a random number in range 0 to count
return randomSelect;
}

May be useful

 How to disable sleep mode when you close the lid on your Mac laptop
 
http://www.simplehelp.net/2008/06/30/how-to-disable-sleep-mode-when-you-close-the-lid-on-your-mac-laptop/

 

Thursday, January 13, 2011

Add badge value to tab bar item


It will display on the specific TabItem's top right corner.

[[self.navigationController tabBarItem] setBadgeValue:@"5"];

Wednesday, January 12, 2011

Search Bar with Clear Background

    UISearchBar* mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,160,320,40)];
    mSearchBar.showsCancelButton = NO;
    mSearchBar.userInteractionEnabled = YES;
    mSearchBar.backgroundColor = [UIColor clearColor];
    mSearchBar.tintColor = [UIColor whiteColor];
    mSearchBar.showsScopeBar = NO;
    [self.view addSubview:mSearchBar];
   
    for (UIView *subview in mSearchBar.subviews)
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subview removeFromSuperview];
            break;
        }
    }

How to handle multiple textFileds

In Load View,

   for(int i=0;i<8;i++)
        {
            UITextField* Field = [[UITextField alloc] initWithFrame:CGRectMake(150,44+35+40*i,160,30)];
            Field.borderStyle = UITextBorderStyleRoundedRect;
            Field.delegate = self;
            Field.tag = i+1;
            [self.view addSubview:Field];
            [mTextFiledsArray addObject:Field];
            [Field release];
        }

Use these Delegate methods,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if(textField.tag == 5) self.view.frame = CGRectMake(0,-20,320,480);
    else if(textField.tag == 7) self.view.frame = CGRectMake(0,-95,320,480);       
    else if(textField.tag == 8) self.view.frame = CGRectMake(0,-130,320,480);       
    [UIView commitAnimations];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    for(int i=0;i<[mTextFiledsArray count];i++)
    {
        UITextField* tf = [mTextFiledsArray objectAtIndex:i];
        if(textField == tf)
        {
            if(i+1 < [mTextFiledsArray count])
                [[mTextFiledsArray objectAtIndex:i+1] becomeFirstResponder];
            else    
            {
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.3];
                self.view.frame = CGRectMake(0,0,320,480);       
                [UIView commitAnimations];
                [textField resignFirstResponder];
            }
        }
    }
    return YES;
}

Tuesday, January 11, 2011

Add Custom Title to UINavigationBar

    UILabel * label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,45,45)] autorelease];
    label.textColor = [UIColor grayColor];
    label.backgroundColor=[UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = label;
    label.text=@"Search"; //CUSTOM TITLE
    [label sizeToFit];
    [label release];

Monday, January 10, 2011

UIApplication Methods

    [UIApplication sharedApplication].statusBarHidden = NO;   // To Hide/Unhide the status bar.
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent; //To set the Style to status bar.
    [UIApplication sharedApplication].statusBarOrientation = UIDeviceOrientationPortrait; //To set the Device Orientation.

We can write
[UIApplication sharedApplication].statusBarHidden     this as
    [[UIApplication sharedApplication] setStatusBarHidden:NO];