Saturday, 8 June 2013

How to print data : PHP

You can use 'echo' to print string, variables, etc. in php. 

Syntax:

        echo "print it...";                       // Output------------->  print it...
        echo '<br/>';                           // prints a new line/enter

        $var_1 = 10;
        $var_2 = 3.14;
        $var_3 = "myString";
        echo $var_1;                           //  Output (number)------->  10
        echo $var_3;                           //  Output (string)------->  myString
        echo "$var_1 + $var_2";         //  Output (sum number)-------> 13.14
       
       echo '$var_1 + $var_2';          //  Output (string)-------> $var_1 + $var_2
       
       echo "$var_1 + $var_2 = ".($var_1 + $var_2);  
       // Output ---------> $var_1 + $var_2 = 13.14


      Note: dot(.) character is used to concant/join strings in php.
 

Friday, 7 June 2013

How to check leap year: Objective C

            If you are an Objective C developer, who is going to make a calender type or something similar applications, then sometimes you may need to find out whether a year is leap year or not. You can use the following function to check whether a year is leap year or not.

Just pass the year to be checked (yearPassed) as an integer value to the following function. Then the function will check for leap year and it will return either 0 or 1 according to the conditions. If it returns 0, then the year is not a leap year. If it returns 1, then the year you passed to the function is a leap year.

Function syntax:

        -(int)leapYearCheck:(int )yearPassed{
               int isLeapYear = 0;
               int yearTocheck = yearPassed;
   
               if (yearTocheck%400 ==0) {
                          isLeapYear = 1;
               }else if (yearTocheck%100 ==0){
                         isLeapYear = 0;
               }else if (yearTocheck%4 ==0){
                         isLeapYear = 1;
               }else if(yearTocheck%4 != 0){
                        isLeapYear = 0;
               }
               return isLeapYear;
         }

Wednesday, 5 June 2013

How to change the image inside UIImageView programmatically: Xcode


In this tutorial, you are going to study how to change the images inside an UIImageView in button click.




 For that, first drag & drop 2 images(say Image1.png & Image2.png)to the project

in.h:

    @interface ViewController : UIViewController
    {
        IBOutlet UIImageView *imageView_;
    }
    -(IBAction)changeToimg1;
    -(IBAction)changeToimg2;
    @end

   
in.m:

    @implementation ViewController

    -(IBAction)changeToimg1
    {
        // This will set Image1.png as the UIImageView image
        UIImage *img1 = [UIImage imageNamed:@"Image1.png"];
        [imageView_ setImage:img1];
    }

    -(IBAction)changeToimg2
    {
        // This will set Image2.png as the UIImageView image
        UIImage *img2 = [UIImage imageNamed:@"Image2.png"];
        [imageView_ setImage:img2];
    }

   
in.xib:
  • Drag and drop UIImageView to the main window
  • Place 2 UIButtons on the screen, name the button labels as Image1 and Image2 respectively for your conveniance
  • Click on the imageView and co
               New Referencing Outlet   --->    imageView_
  • Click on Button1 and connect
               Touch Up Inside             ---->    changeToimg1
  •  Click on Button2 and connect   
               Touch Up Inside          ---->   changeToimg2



Now save and Run the program. Click on the buttons, and you can see the images changing.

Tuesday, 4 June 2013

How to call a function using NSTimer: Xcode

NSTimer can be used to call functions with a scheduled delay. You can control the interval and repeat mode (whether the function need to be called per every delay intervals or not) also. The syntax is very simple, and is given below:

Syntax:

         [NSTimer scheduledTimerWithTimeInterval:0.60
                                                                   target:self
                                                                   selector:@selector(myFunction)
                                                                   userInfo:nil
                                                                   repeats:NO];

How to create an UITableView: Xcode

Overview: 
     An instance of UITableView (or simply, a table view) is a means for displaying and editing hierarchical lists of information.



xib:

 Drag and drop tableView.
 Connect  tableView : dataSource   ---->     viewController/File's owner
                                  deligate         ---->     viewController/File's owner

in .h:

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
     NSArray *exercises;
    UITableViewCell *cell;
}
@end

in .m:

@implementation ViewController
     
- (void)viewDidLoad
{
  //-----------create table list array-----------//
     exercises = [[NSArray alloc]initWithObjects:@"Name 1",@"Name 2",@"Name 3",@"Name 4",
                             @"Name 5",@"Name 6",@"Name 7",@"Name 8",@"Name 9",@"Name 10",nil];
   
     [super viewDidLoad];
}

 //Add table view delegates //

//------------delegate that decides the number of rows in table view--------------//        
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
      return exercises.count;
}

//------------delegate decides the content of each table cell--------------//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      //-----------create a cell-----------//
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   
     //-----------fill it with contents-----------//
        cell.textLabel.text = [exercises objectAtIndex:indexPath.row];
           
     //-----------return it-----------//
        return cell;
}

//------------To get the touched cell val/name --------------//
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSLog(@"Selected Row    %@",[exercises objectAtIndex:indexPath.row]);
}

//------------Table view swipe delete deligate(optional)------------//

                            For using this method, you have to add and connect IBOutlet to your table view (say myEditableTable), and load your table from an NSMutableArray (say exercises_Mutable). Then use the following method:


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Repsond to delete
NSLog(@”Delete-à %@”, [exercises_Mutable objectAtIndex:indexPath.row]);
[exercises_Mutable removeObjectAtIndex: indexPath.row] ;
[myEditableTable reloadData];

    }    
}

Here, the ‘[exercises_Mutable removeObjectAtIndex: indexPath.row]’ will remove the clicked indexed element from your Mutable array, and ‘[myEditableTable reloadData]’ will reload the table with the changed array.

Optional : And if you want to change the text of the Delete button you can use the following syntax*/
                  tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:


//------------To add an image to UITableViewCell(optional)------------//
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]];
  }



//------------To change UITable view separator color and style(optional)------------//
 Separator is the straight line that separates each cell in a tableView. You have control over it's style and color. You can change those properties as follows:

        myTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
        myTableView.separatorColor = [UIColor brownColor];

How to create an alert view programmatically: cocos2d

Overview:

Use an activity indicator to show that a task is in progress. An activity indicator appears as a “gear” that is either spinning or stopped.

You control when an activity indicator animates by calling the startAnimating and stopAnimating methods. To automatically hide the activity indicator when animation stops, set the hidesWhenStopped property to YES.
 







  You can create the alert view programmatically as below:

// in .h //

    UIImageView *bgView;
    UIActivityIndicatorView *spin;

// in .m //

//  Call the following method in init/when the scene loads:
 -(void)createIndicator
    {
        spinnerAdded = TRUE;
        bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
        bgView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8];
        bgView.layer.cornerRadius = 8.0;
        [[[CCDirector sharedDirector] openGLView] addSubview:bgView];
           
        spin = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [bgView addSubview:spin];
        [spin setHidesWhenStopped:YES];
        spin.center = CGPointMake(70, 70);
        [spin startAnimating];
        [spin release];
           
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 105, bgView.frame.size.width, 15)];
        lbl.center = CGPointMake(bgView.frame.size.width/2, lbl.center.y);   
        [bgView addSubview:lbl];   
        [lbl setBackgroundColor:[UIColor clearColor]];   
        lbl.textAlignment = UITextAlignmentCenter;   
        [lbl setFont:[UIFont boldSystemFontOfSize:14]];   
        [lbl setTextColor:[UIColor whiteColor]];   
        lbl.text = @"Loading...";
    }



startAnimating:

     Starts the animation of the progress indicator. When the progress indicator is animated, the gear spins to indicate indeterminate progress. The indicator is animated until stopAnimating is called.

 Syntax:

                  [spin startAnimating];


stopAnimating:

     Stops the animation of the progress indicator.



  Syntax:

           [spin stopAnimating];
           [bgView removeFromSuperview];


UIActivityIndicatorStyle:

   The visual style of the progress indicator.

typedef enum {
   UIActivityIndicatorViewStyleWhiteLarge,
   UIActivityIndicatorViewStyleWhite,
   UIActivityIndicatorViewStyleGray,
} UIActivityIndicatorViewStyle;

How to check whether there is internet connection with the device: cocos2d/Xcode

You can check whether there is internet connection with the device/not with the help of following code:

     // in .h File //

        Import Reachability class.
       


    // in .m File //

        if([self connected] )
        {
            NSLog(@"Internet connected\n");
        }
       
        - (BOOL)connected
        {
            Reachability *reachability = [Reachability reachabilityForInternetConnection];
            NetworkStatus networkStatus = [reachability currentReachabilityStatus];
            return !(networkStatus == NotReachable);
        }