Dailycode.info

Short solution for short problems

iOS : SUDZC cannot serialize date from .Net WCF

SUDZC is working fine for me. (That rhymes :-) Strangely enough all dates are deserialized correct on the IPAD. All dates coming from the database that is. But when filled a date directly from the .Net web service, it was not deserializing. The reason is that the date format was different. WCF serializes the date into a larger format.

Dates coming from the database in the entities were in this format:

 

2013-01-04T13:19:55.253

 

No problem there. But then when I filled a date into the same entity directly in the web service code, the date looked like this after serialization:

 

2013-03-20T15:31:20.560557+01:00

 

So I improved the deserialization method for dates a little for SUDZC to handle these dates. You can find the method in the SOAP.m class (Do not forget to remove the NSLogs after you're done):

// Converts a string to a date.

+ (NSDate*) dateFromString: (NSString*) value

{

NSLog(@"val: %@",value);

    if([value rangeOfString:@"T"].length != 1) {

value = [NSString stringWithFormat:@"%@T00:00:00.000", value];

}

if([value rangeOfString:@"."].length != 1) {

value = [NSString stringWithFormat:@"%@.000", value];

}

    if (value.length > 23)

        value = [value substringToIndex:23];

    

if(value == nil || [value isEqualToString:@""]) { return nil; }

NSDate* outputDate = [[Soap dateFormatter] dateFromString: value];

    NSLog(@"output: %@",outputDate);

return outputDate;

}

 


iOS : scroll TextView or TextField

Very soon you will bump into the keyboard coming in front of your TextField or TextView.

I found a good solution on stack overflow and improved it a little. It didn't support orientation changes.

First you'll need to register the keyboard shown and hidden methods in your ViewDidLoad:

...
[selfregisterForKeyboardNotifications];
...
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenterdefaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenterdefaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

 

Now for your TextFields (not needed for UITextView) implement these methods from the delegate UITextFieldDelegate:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}
 
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField =nil;
}

They need the variable active field to be declared somewhere in your implementation:

UIView * activeField;

Then the trick is implementing these to methods. They are called when the keyboard is shown or hidden:

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIDeviceOrientation orientation = [[UIDevicecurrentDevice] orientation];
    UIEdgeInsets contentInsets;
    CGFloat height;
    if (UIInterfaceOrientationIsPortrait(orientation))
    {
        height = kbSize.height;
    }
    else
    {
        height =  kbSize.width;
    }
    contentInsets = UIEdgeInsetsMake(0.0, 0.0, height, 0.0);
    self.scrollview.contentInset = contentInsets;
    self.scrollview.scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= height;
    CGPoint origin = activeField.frame.origin;
    origin.y -= self.scrollview.contentOffset.y;
    if (!CGRectContainsPoint(aRect, origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-(aRect.size.height));
        [self.scrollview setContentOffset:scrollPoint animated:YES];
    }
}
 
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.scrollview.contentInset = contentInsets;
    self.scrollview.scrollIndicatorInsets = contentInsets;
}

 


iOS: Format UITextView as UITextField

It's really simple and strange that it's not made by default.

I found a good solution that works perfect and you can see no difference between TextField and TextView, except for the height.

I made a small function that will style your TextView like a TextField:

+(void) MakeTextField:(UITextView *) vw

{

    vw.backgroundColor = [UIColorclearColor];

    UIImageView *borderView = [[UIImageViewalloc] initWithFrame: CGRectMake(0, 0, vw.frame.size.width, vw.frame.size.height)];

    borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    UIImage *textFieldImage = [[UIImageimageNamed:@"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];

    borderView.image = textFieldImage;

    [vw addSubview: borderView];

    [vw sendSubviewToBack: borderView];

}

Just call this function on the DidLoad and pass the TextViews you want to format:

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    [GeneralZPFunctionsMakeTextField:self.txtInformation];

}

You will also need this picture and call it TextField.png:

Thats it! Below you see 2 examples of UITextView that look like a UITextField!

Source: http://stackoverflow.com/questions/1824463/how-to-style-uitextview-to-like-rounded-rect-text-field


iOS: tableView show different row colors every second row

Just to have a little bit nicer looking tableviews, I implement this little function that show a different color every record:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (fmod(indexPath.row,2)==0)

    {

        cell.backgroundColor =  [UIColor colorWithRed:210/255.0f green:210/255.0f blue:210/255.0f alpha:1];

    }

}