This gist contains all the code snippets for the blog post "NSInvocation" at http://www.apokrupto.com/blog-1/2016/3/14/ogbhxwpj9n4qh79edsvpdyvbpklr3w
Last active
June 11, 2016 12:40
-
-
Save warren-gavin/abb0b6e2628e16977b240ba4375e4173 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@interface ViewController(Refactor) | |
@property (nonnull, readonly) NSArray *cellInfo; | |
@end | |
@implementation ViewController (Refactor) | |
- (NSArray *)cellInfo { | |
return @[ @{ @"text": @"Awful code", @"block": ^{ [self showAlert]; } }, | |
@{ @"text": @"Really awful code", @"block": ^{ [self segue]; } } ]; | |
} | |
- (void)showAlert { | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad" | |
message:@"Terrible" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop" | |
style:UIAlertActionStyleCancel | |
handler:NULL]]; | |
[self presentViewController:alert animated:YES completion:NULL]; | |
} | |
- (void)segue { | |
[self performSegueWithIdentifier:@"Unforgivable" sender:self]; | |
} | |
@end | |
@implementation ViewController | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.cellInfo.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
void (^operation)() = self.cellInfo[indexPath.row][@"block"]; | |
operation(); | |
} | |
@end |
This file contains hidden or 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
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
if (indexPath.row == 0) { | |
cell.textLabel.text = @"Awful code"; | |
} | |
else if (indexPath.row == 1) { | |
cell.textLabel.text = @"Really awful code"; | |
} | |
else if (indexPath.row == 2) { | |
cell.textLabel.text = @"Make it stop"; | |
} | |
else if (indexPath.row == 3) { | |
cell.textLabel.text = @"Ohmygodwereallgoingtodie"; | |
} | |
else { | |
cell.textLabel.text = @"You don't do this, do you?"; | |
} | |
return cell; | |
} |
This file contains hidden or 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
@implementation ViewController | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return 2; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
if (indexPath.row == 0) { | |
cell.textLabel.text = @"Awful code"; | |
} | |
else { | |
cell.textLabel.text = @"Really awful code"; | |
} | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (indexPath.row == 0) { | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad" | |
message:@"Terrible" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop" | |
style:UIAlertActionStyleCancel | |
handler:NULL]]; | |
[self presentViewController:alert animated:YES completion:NULL]; | |
} | |
else { | |
[self performSegueWithIdentifier:@"Unforgivable" sender:self]; | |
} | |
} | |
@end |
This file contains hidden or 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
@implementation NSInvocation (Apokrupto) | |
+ (instancetype)invocationForSelector:(SEL)selector target:(id)target { | |
NSMethodSignature *signature = [target methodSignatureForSelector:selector]; | |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; | |
invocation.selector = selector; | |
invocation.target = target; | |
return invocation; | |
} | |
@end | |
@interface ViewController(Refactor) | |
@property (nonnull, readonly) NSArray *cellInfo; | |
@end | |
@implementation ViewController (Refactor) | |
- (NSArray *)cellInfo { | |
return @[ @{ @"text": @"Awful code", | |
@"selector": [NSInvocation invocationForSelector:@selector(showAlert) target:self] }, | |
@{ @"text": @"Really awful code", | |
@"selector": [NSInvocation invocationForSelector:@selector(segue) target:self] } ]; | |
} | |
- (void)showAlert { | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad" | |
message:@"Terrible" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop" | |
style:UIAlertActionStyleCancel | |
handler:NULL]]; | |
[self presentViewController:alert animated:YES completion:NULL]; | |
} | |
- (void)segue { | |
[self performSegueWithIdentifier:@"Unforgivable" sender:self]; | |
} | |
@end | |
@implementation ViewController | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.cellInfo.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
NSInvocation *invocation = self.cellInfo[indexPath.row][@"selector"]; | |
[invocation invoke]; | |
} | |
@end |
This file contains hidden or 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
@implementation ViewController (Refactor) | |
- (void)showAlert { | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad" | |
message:@"Terrible" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop" | |
style:UIAlertActionStyleCancel | |
handler:NULL]]; | |
[self presentViewController:alert animated:YES completion:NULL]; | |
} | |
- (void)segue { | |
[self performSegueWithIdentifier:@"Unforgivable" sender:self]; | |
} | |
@end | |
@implementation ViewController | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return 2; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
if (indexPath.row == 0) { | |
cell.textLabel.text = @"Awful code"; | |
} | |
else { | |
cell.textLabel.text = @"Really awful code"; | |
} | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (indexPath.row == 0) { | |
[self showAlert]; | |
} | |
else { | |
[self segue]; | |
} | |
} | |
@end |
This file contains hidden or 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
@interface ViewController(Refactor) | |
@property (nonnull, readonly) NSArray *cellInfo; | |
@end | |
@implementation ViewController (Refactor) | |
- (NSArray *)cellInfo { | |
return @[ @{ @"text": @"Awful code", @"selector": @selector(showAlert) }, | |
@{ @"text": @"Really awful code", @"selector": @selector(segue) } ]; | |
} | |
- (void)showAlert { | |
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Bad" | |
message:@"Terrible" | |
preferredStyle:UIAlertControllerStyleAlert]; | |
[alert addAction:[UIAlertAction actionWithTitle:@"Please stop" | |
style:UIAlertActionStyleCancel | |
handler:NULL]]; | |
[self presentViewController:alert animated:YES completion:NULL]; | |
} | |
- (void)segue { | |
[self performSegueWithIdentifier:@"Unforgivable" sender:self]; | |
} | |
@end | |
@implementation ViewController | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.cellInfo.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; | |
cell.textLabel.text = self.cellInfo[indexPath.row][@"text"]; | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
[self performSelector:self.cellInfo[indexPath.row][@"selector"]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment