Skip to content

Instantly share code, notes, and snippets.

View mikeabdullah's full-sized avatar

Mike Abdullah mikeabdullah

View GitHub Profile
@mikeabdullah
mikeabdullah / gist:8ecff4375c5732ca7187
Last active August 29, 2015 14:18
Manually created NSLayoutConstraint
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.view1
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view2
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0];
@mikeabdullah
mikeabdullah / gist:d2216fdb378a71c50626
Last active August 29, 2015 14:14
Creating a Hit List URL with + symbols in query items properly encoded
- (NSURL *)hitListURLWithTaskTitle:(NSString *)title {
// Basic URL as a starting point
NSURLComponents *components = [NSURLComponents componentsWithString:@"thehitlist:///inbox/tasks"];
components.queryItems = @[
[NSURLQueryItem queryItemWithName:@"method" value:@"POST"],
[NSURLQueryItem queryItemWithName:@"title" value:title]
];
// NSURLQueryItem has encoded any spaces as %20 which is fine
@mikeabdullah
mikeabdullah / gist:cc7bcb8a352a7ece98da
Last active January 5, 2017 08:41
Decoding URL query items for schemes where + is to be treated as a space
NSURLComponents *components = [NSURLComponents componentsWithString:input];
// Re-encode + symbols so NSURLQueryItem recognises them as spaces
components.percentEncodedQuery = [components.percentEncodedQuery
stringByReplacingOccurrencesOfString:@"+"
withString:@"%20"];
NSArray *items = components.queryItems;
// Enumerate and handle the items
@mikeabdullah
mikeabdullah / gist:0bfa45534dc0b75e32a8
Last active August 29, 2015 14:13
Date parsing with NSDataDetector
NSDataDetector *detector = [NSDataDetector
dataDetectorWithTypes:NSTextCheckingTypeDate
error:NULL];
NSTextCheckingResult *result = [detector
firstMatchInString:string
options:0
range:NSMakeRange(0, string.length)];
return result.date;
@mikeabdullah
mikeabdullah / gist:87a26ef65b90cb238c97
Created December 19, 2014 18:30
Restoring view controllers in a paged UI
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.firstViewController forKey:@"viewController1"];
[coder encodeObject:self.secondViewController forKey:@"viewController2"];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
@mikeabdullah
mikeabdullah / gist:20813743ccbee77223e5
Created December 19, 2014 18:13
Restoring selected tab of paged table view controllers
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeInteger:self.mode forKey:@"mode"];
}
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
self.mode = [coder decodeIntegerForKey:@"mode"];
}
@mikeabdullah
mikeabdullah / MainViewController.m
Last active August 29, 2015 14:11
Encoding child view controllers in custom container
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.contentViewController
forKey:@"contentViewController"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedRowIndexPath) {
[self.transitionCoordination animateAlongsideTransitionInView:self.tableView animation:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.tableView deselectRowAtIndexPath:selectedRowIndexPath animated:YES];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
@mikeabdullah
mikeabdullah / MyTableViewCell.h
Last active August 29, 2015 14:10
Connecting cell to controller using a block
@interface MyTableViewCell
@property(nonatomic, copy) void (^checkboxHandler)(void);
@end
@mikeabdullah
mikeabdullah / MyTableViewCell.m
Last active August 29, 2015 14:10
Connecting cell to controller by responder chain properly
@implementation MyTableViewCell
- (IBAction)checkboxPressed:(UIButton *)checkbox {
// Send onwards up the responder chain
id target = [self targetForAction:@selector(cellCheckboxPressed:)
withSender:self];
[target performSelector:@selector(cellCheckboxPressed:)
withObject:self];
}