Created
September 18, 2013 19:36
-
-
Save bazscott/6614381 to your computer and use it in GitHub Desktop.
A collection of the code snippets I use in Dash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==================================================== | |
// SINGLETON | |
// ==================================================== | |
+ (__class__ *)__accessor__ { | |
static dispatch_once_t pred; | |
static __class__ *__singleton__ = nil; | |
dispatch_once(&pred, ^{ | |
__singleton__ = [[__class__ alloc] init]; | |
}); | |
return __singleton__; | |
} | |
// ==================================================== | |
// NOTIFICATIONS | |
// ==================================================== | |
// Receive a notification | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(receivedNSNotification:) | |
name:@"__notificationname__" | |
object:nil]; | |
- (void)receivedNSNotification:(NSNotification *) notification { | |
if ([[notification name] isEqualToString:@"__notificationname__"]) { | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"__notificationname__" object:nil]; | |
NSLog (@"Successfully received notification: __notificationname__"); | |
} | |
} | |
// Broadcast a notification | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"__notificationname__" object:self]; | |
// ==================================================== | |
// MAIN QUEUE | |
// ==================================================== | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
}); | |
// ==================================================== | |
// DELEGATE | |
// ==================================================== | |
// Interface | |
@class __class__; | |
@protocol __delegatename__ <NSObject> | |
@optional | |
- (void)__optionalmethod__:(BOOL)success; | |
@required | |
- (void)__requiredmethod__:(BOOL)success; | |
@end | |
@interface __class__ : __superclass__ { | |
id <__delegatename__> delegate; | |
} | |
@property (nonatomic,retain) id <__delegatename__> delegate; | |
// Implementation | |
@synthesize delegate; | |
if ([delegate respondsToSelector:@selector(__requiredmethod__:)]){ | |
[delegate __requiredmethod__:YES]; | |
} | |
// ==================================================== | |
// DELAY | |
// ==================================================== | |
[self performSelector:@selector(__methodtocall__) withObject:self afterDelay:__delayint__]; | |
// ==================================================== | |
// CONSTANTS | |
// ==================================================== | |
// Interface | |
// e.g. kTVManagerNotifyTVDidConnect | |
extern NSString *const __constantname__; | |
// Implementation | |
NSString *const __constantname__ = @"__constanttext__"; | |
// ==================================================== | |
// KEYBOARD ACCESSORY VIEW | |
// ==================================================== | |
// Interface | |
<UITextViewDelegate> | |
@property (strong, nonatomic) UIToolbar *fieldAccessoryView; | |
// Implementation | |
// viewDidLoad or somewhere like that | |
[self createAccessoryView]; | |
[self.__textview__ setInputAccessoryView:self.fieldAccessoryView]; | |
// Down the bottom, probably | |
#pragma mark - Keyboard methods | |
- (void)createAccessoryView { | |
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0); | |
self.fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame]; | |
self.fieldAccessoryView.tag = 200; | |
// | |
self.fieldAccessoryView.barStyle = UIBarStyleDefault; | |
// | |
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace | |
target:nil | |
action:nil]; | |
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone | |
target:self | |
action:@selector(doneButtonTapped)]; | |
// | |
[self.fieldAccessoryView setItems:[NSArray arrayWithObjects:spaceButton, doneButton, nil] animated:NO]; | |
} | |
-(void) doneButtonTapped { | |
[self.__textview__ resignFirstResponder]; | |
} | |
- (BOOL)textFieldShouldReturn:(UITextField *)textField { | |
[textField resignFirstResponder]; | |
return YES; | |
} | |
- (void)textViewDidEndEditing:(UITextView *)textView { | |
[textView resignFirstResponder]; | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
[self.view endEditing:YES]; | |
[super touchesBegan:touches withEvent:event]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment