Tuesday, April 23, 2013

iOS Scrap Book


Setting the title of a ViewController:

self.title = @"Clock In Time";

Setting the field placeholder:

signOutTimeField.placeholder = @"Time to Clock Out of Work";

Bending the corners of  a button

cancelJobButton.layer.cornerRadius = 8;
cancelJobButton.layer.borderWidth = 1;
cancelJobButton.layer.borderColor = [UIColor grayColor].CGColor;
cancelJobButton.clipsToBounds = YES;

Adding a Return Key to the Keyboard and Calling a method ("hideKeyBoard:") after the Keyboard Retracts

emailAddressField.returnKeyType=UIReturnKeyDone;
[emailAddressField addTarget:self action:@selector(hideKeyBoard:)
forControlEvents:UIControlEventEditingDidEndOnExit];

Defining a Method for Keyboard Retraction

-(void)hideKeyBoard: (id) sender
{
//Hiding the Keyboard
if ([sender tag] == 0) {
[emailAddressField resignFirstResponder];
}
else if ([sender tag] == 1) {
[passwordField resignFirstResponder];
}

[UIView beginAnimations:nil context:NULL];

//Setting the animation duration
[UIView setAnimationDuration:1];
CGRect rect = self.view.frame;

//Resetting the origin to 0
rect.origin.y = 0;
self.view.frame = rect;
[UIView commitAnimations];
}

Getting the ViewController to 'Roll' up and down when the Keyboard Retracts:

- (void)textFieldBegin:(NSNotification *)notif
{
UITextField *obj= [notif object];NSNumber *offsetValue=[[NSNumber alloc]
initWithFloat:obj.frame.origin.y];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
//Get a reference to the frame
CGRect rect = self.view.frame;
//Set the origin to a new value
rect.origin.y = 60.0-[offsetValue floatValue];
//Adjust the views height
rect.size.height += [offsetValue floatValue]-60.0;
//Commit the changes to the current frame
self.view.frame = rect;
//Commit the animations
[UIView commitAnimations];
}

Adding an Observer and Removing an Observer when the view Appears and a View Disappears

- (void)viewWillDisappear:(BOOL)animated
{
// unregister for textfield notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidBeginEditingNotification object:nil];
}

-(void) viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
(textFieldBegin:) name:UITextFieldTextDidBeginEditingNotification object:self.view.window];
[super viewWillAppear:animated];
}

Building an Alert


alert = [[UIAlertView alloc]
initWithTitle:@"Unverified" 
message:@"Please enter a new name and password." 
delegate:nil cancelButtonTitle:@"OK" 
otherButtonTitles:nil];
 
[alert show];
[alert release];

Trimming the Whitespace from a NSString


cellPhoneNumberString = [cellPhoneNumberTextField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];

Accessing a Restful Service:

    NSString *urlString = @"http://www.samplesite.com/dtdOrdersExist.pl?";
    NSString *queryString = @"";
    queryString = [queryString stringByAppendingString:@"parameter1="];
    queryString = [queryString stringByAppendingString:p1];
    queryString = [queryString stringByAppendingString:@"parameter2="];
    queryString = [queryString stringByAppendingString:p2];
    
    // Step 5:  Make the RESTful call.
    
    urlString = [urlString stringByAppendingString:queryString];
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
    
    NSLog(@"ConfirmationCodeViewController URL String: %@", urlString);
    
    // {cellPhoneNumberConfirmed => $cellPhoneNumberConfirmed, cellPhoneNumber => $cellPhoneNumber, customerID => $customerID};
    
    
    // RESTFUL CODE BLOCK A
    
    
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *dataURL = [NSData dataWithContentsOfURL:url];
    NSDictionary *myDict = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
    
    // Step 6:  Retrieve the data from the RESTful call
    
    NSString* numberOfOrders = [myDict objectForKey:@"numberOfOrders"];
    
    NSLog(@"Post JSON: Verified jsonCustomerID: %@", numberOfOrders);


    return numberOfOrders;


Pushing a New ViewController:


CourierViewController *cvc = [[CourierViewController alloc
  initWithNibName:@"CourierViewController" 
  bundle:nil];

[self.navigationController pushViewController:cvc animated:YES];
[cvc release];

Trimming Leading and trailing Whitespaces  off NSString:


NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]

Deleting all objects from Core Data:


-(NSManagedObjectContext *)deleteCustomerObject: (NSManagedObjectContext *) context
{
    
    NSLog(@"deleteCustomerObject");
// self.navigationController = [[UINavigationController alloc] initWithNibName:@"Go_For_MeViewController" bundle:nil];
// NSManagedObjectContext* context = [fetchedResultsController managedObjectContext];
if (context == nil)
    {
context = [(Go_For_MeAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@",   context);
}
NSLog(@"GFMVC: Test Point 1");
    
// INPUT THE ENTITY NAME SLATED FOR DELETION HERE
    
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"CustomerInformation"
  inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
// NSPredicate *pred = [NSPredicate predicateWithFormat:@"(loggedIn == 1)"];
// [request setPredicate:pred];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
    /*
    entityDesc =  [NSEntityDescription entityForName:@"jobData"
  inManagedObjectContext:context];
[request setEntity:entityDesc];
pred = [NSPredicate predicateWithFormat:@"(jobCompleted == NO)"];
[request setPredicate:pred];
    
NSError *jobsError = nil;
NSArray *jobsResults = [context executeFetchRequest:request error:&jobsError];
    */
    
    if (results != nil && [results count] >= 0)
    {
NSLog(@"GFMVC:  Valid Request Results");
int size = [results count];
NSLog(@"GFMVC: Number of Results: %d", size);
        
        // DELETING THE OBJECTS IN THE MANAGED OBJECT CONTEXT.
for (NSManagedObject *newManagedObject in results)
        {
// [self printContext:newManagedObject];
[context deleteObject:newManagedObject];
}
        [self saveContext:context];
        return context;
        
}
else if (results == nil)
{
        
        [self saveContext:context];
        return context;
}

    [self saveContext:context];
    return context;
    
}



How to Save an object with a relationship.  BOOYAH!
        
[customerManagedObject setValue:ordersManagedObject forKey:@"customerToOrders"];

-- customerManagedObject -- the object that is the parent

-- ordersManagedObject -- the object that is the child object

-- customerToOrders -- the name of the relationship object


Setting up UIPickerView:

UIPickerView Set Up

Making UIPickerView Appear When UITextField is Selected

Printing a Date and a Time:


      timeLoggedIn = [NSDateFormatter localizedStringFromDate:[managedObj valueForKey:@"timeLoggedIn"]
      dateStyle:NSDateFormatterShortStyle
      timeStyle:NSDateFormatterFullStyle];

Changing ViewControllers with Storyboard Programatically:


If you're in a Navigation Controller:
ViewController *viewController = [[ViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
or if you just want to present a new view:
ViewController *viewController = [[ViewController alloc] init];    
[self presentViewController:viewController animated:YES completion:nil];

MySQL:

Alter a column:  Dropping the NOT NULL provision:


ALTER TABLE tbl_name MODIFY columnA varchar(6) NULL DEFAULT NULL;





No comments:

Post a Comment