Thursday, 28 March 2013

how to animate a UIView from left to right or as per your requirement in iOS .



You can add animation to your UIView or any sub view

ur_view.frame=CGRectMake(248, -420, 500, 414);  //your view start position
    
 [UIView animateWithDuration:0.5f
                          delay:0.0f
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [ur_view setFrame:CGRectMake(248, 30, 500, 414)];   // last position
                     }
                     completion:nil]; 

How to add a video clip in a ios app.

First of all add MediaPlayer/MediaPlayer framework into ur project.

Then drag your video into project resource folder.

then add the below code in viewDidLoad() method or as per your requirement:


- (void)viewDidLoad
{

 [super viewDidLoad];




 NSString *url = [[NSBundle mainBundle]
                     pathForResource:@"video"
                     ofType:@"mp4"];
    
    player =[[MPMoviePlayerController alloc]
             initWithContentURL:[NSURL fileURLWithPath:url]];
    player.controlStyle=NO;                      // to add or remove controls eg. stop / play
    player.movieSourceType = MPMovieSourceTypeFile;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    
    //---play movie---//
    player.view.frame = CGRectMake(0, 0, 1024  , 768);  
    player.repeatMode=NO;
    
    [self.view addSubview:player.view];  // to add player view on ur view
    [player play];

}

IF you want to perform some action just after video completion then add below delegate method:



-(void)myMovieViewFinishedCallback:(NSNotification*)theNotification
{
    MPMoviePlayerController *moviePlayer=[theNotification object];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];

    [moviePlayer.view removeFromSuperview];     // to remove player view

    
    [self yourMethod];                                         // call ur method
}


you must add delegate into your interface declaration

eg.

@interface ViewController : UIViewController <MPMediaPickerControllerDelegate>


Thursday, 14 March 2013

To find unique elements of array from a repeated elements array.


NSArray* repeatedArray=[[NSArray alloc]init];

repeatedArray=[@"1,2,2,2,2,3,3,3,3,4,4,1,1,1,1,5,5,5,5,5,5"]; //example 

NSMutableArray *uniqueArray = [[NSMutableArray alloc]init];

    for(NSNumber *obj in repeatedArray) //NSNumber it depends on ur object type
    {
        if(![uniqueArray containsObject:obj])
        {
            [uniqueArray addObject:obj];
        }
    }

OUTPUT: uniqueArray=[1,2,3,4,5];  //THATS what we want

Thursday, 7 March 2013

How to put queries in CORE DATA to fetch specific attribute values


// Condition to check for form id and instance

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"formId == %i && instanceNo == %d && questionId == %i", [form.formId intValue],instancevalue,[qq.formQuestionId intValue]];

[request setPredicate:predicate];

// WE use predicate for sql queries to fetch specific data 

How to retrieve or fetch data from CORE DATA in iOS for a particular entity.


NSManagedObjectContext *context = [[SharedVars sharedInstance]localContext];
NSError *error = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];

// Condition to check for form id and instance
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"formId == %i && instanceNo == %d", [form.formId intValue],instancevalue];
    [request setPredicate:predicate];
//   NSPredicate for queries 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"UnsavedForm" inManagedObjectContext:context];
[request setEntity:entity];
    
// Order   by   questionId.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"questionId" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
//   NSSortDescriptor for sorting purpose

NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil)
    {
// Handle the error.
    }
//mutableFetchResults contains the core data elements , you can handle as per your //requirement as i am doing for my specific requirement below.
    
NSMutableArray *Qsetter=[[NSMutableArray alloc]init];
NSMutableArray *Asetter=[[NSMutableArray alloc]init];
    for(int objCount=0;objCount<[mutableFetchResults count];objCount++)
    {
   YourEntityClass=[mutableFetchResults objectAtIndex:objCount];
  
     //   NSLog(@" form id : %@ question id : %@ answers : %@ instance No ; %@",unsavedform.formId,unsavedform.questionId,unsavedform.answers,unsavedform.instanceNo);
        [Qsetter addObject:unsavedform.questionId];
        [Asetter addObject:unsavedform.answers];
    }

How to sort the retrieve data from CORE DATA in IOS



// Order   by   questionId.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Your_Attribute_of_entity" ascending:YES]; //here you can set ascending order or descending or not any order

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[request setSortDescriptors:sortDescriptors];

How to delete attributes value for a Entity in CORE DATA in IOS



    NSManagedObjectContext *context;
    
    NSFetchRequest * request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"YourEntityClass" inManagedObjectContext:context]];
    [request setIncludesPropertyValues:NO]; //only fetch the managedObjectID
    
    NSError * error = nil;
    NSArray * formDataDel = [context executeFetchRequest:request error:&error];
    
    //error handling goes here
    for (NSManagedObject * data in formDataDel) 
    {
        [context deleteObject:data];  //DELETING HERE
    }
    NSError *saveError = nil;
    [context save:&saveError];

UIAlertView delegate to perform specific actions on button click of Alert Box




- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex)
    {
        // cancel clicked ...do your action
    }
    else if (buttonIndex == 1)
    {
      // 1st button  clicked ...do your action
    }
     else if (buttonIndex == 2)
    {
      // 2nd button  clicked ...do your action
    }
}
// remember to add uialertViewDelegate 

How to insert attributes data for a entity in CORE DATA in IOS



NSManagedObjectContext *context = [[SharedVars sharedInstance]localContext];
YourEntityClass  *event = (UnsavedForm *)[NSEntityDescription insertNewObjectForEntityForName:@"EntityNameClass" inManagedObjectContext:context];
               
        [event setQuestionId:qArray];
        [event setFormId:[NSNumber numberWithInt:[form.formId intValue]]];
        [event setAnswers:answerString];
        [event setUserId:userID];
        [event setInstanceNo:[NSNumber numberWithDouble:pageStepper.value]];