Implemented NSBlocks in lieu to the newly deprecated delegate methods

This commit is contained in:
Max Kramer
2012-12-27 21:45:13 +00:00
parent 2210243a97
commit c78bce5ce7
7 changed files with 180 additions and 10 deletions

View File

@@ -8,15 +8,32 @@
#import <UIKit/UIKit.h>
#define DEPRECATED __attribute__ ((deprecated))
typedef enum {
STLinkActionTypeAccount,
STLinkActionTypeHashtag,
STLinkActionTypeWebsite
} STLinkActionType;
typedef void(^STLinkCallbackBlock)(STLinkActionType actionType, NSString *link);
/*
* Deprecated Delegate Callbacks.
* Please use NSBlocks instead.
*/
@protocol STLinkProtocol <NSObject>
@optional
- (void)twitterAccountClicked:(NSString *)link;
- (void)twitterHashtagClicked:(NSString *)link;
- (void)websiteClicked:(NSString *)link;
- (void)twitterAccountClicked:(NSString *)link DEPRECATED;
- (void)twitterHashtagClicked:(NSString *)link DEPRECATED;
- (void)websiteClicked:(NSString *)link DEPRECATED;
@end
@interface STTweetLabel : UILabel
{
NSMutableArray *touchLocations;
@@ -27,7 +44,8 @@
@property (nonatomic, strong) UIFont *fontHashtag;
@property (nonatomic, strong) UIColor *colorLink;
@property (nonatomic, strong) UIColor *colorHashtag;
@property (nonatomic, strong) id<STLinkProtocol> delegate DEPRECATED;
@property (nonatomic, strong) id<STLinkProtocol> delegate;
@property (nonatomic, copy) STLinkCallbackBlock callbackBlock;
@end

View File

@@ -160,6 +160,13 @@
if ([_delegate respondsToSelector:@selector(twitterAccountClicked:)]) {
[_delegate twitterAccountClicked:url];
}
if (_callbackBlock != NULL) {
_callbackBlock(STLinkActionTypeAccount, url);
}
}
else if ([[touchWords objectAtIndex:idx] hasPrefix:@"#"])
{
@@ -167,6 +174,12 @@
if ([_delegate respondsToSelector:@selector(twitterHashtagClicked:)]) {
[_delegate twitterHashtagClicked:url];
}
if (_callbackBlock != NULL) {
_callbackBlock(STLinkActionTypeHashtag, url);
}
}
else if ([[touchWords objectAtIndex:idx] hasPrefix:@"http"])
{
@@ -175,6 +188,13 @@
if ([_delegate respondsToSelector:@selector(websiteClicked:)]) {
[_delegate websiteClicked:url];
}
if (_callbackBlock != NULL) {
_callbackBlock(STLinkActionTypeWebsite, url);
}
}
}
}];

View File

@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0450"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "254E4346167E7475004191B6"
BuildableName = "STTweetLabelExample.app"
BlueprintName = "STTweetLabelExample"
ReferencedContainer = "container:STTweetLabelExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "254E4346167E7475004191B6"
BuildableName = "STTweetLabelExample.app"
BlueprintName = "STTweetLabelExample"
ReferencedContainer = "container:STTweetLabelExample.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "254E4346167E7475004191B6"
BuildableName = "STTweetLabelExample.app"
BlueprintName = "STTweetLabelExample"
ReferencedContainer = "container:STTweetLabelExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "254E4346167E7475004191B6"
BuildableName = "STTweetLabelExample.app"
BlueprintName = "STTweetLabelExample"
ReferencedContainer = "container:STTweetLabelExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>STTweetLabelExample.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>254E4346167E7475004191B6</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@@ -8,10 +8,8 @@
#import <UIKit/UIKit.h>
#import "STTweetLabel.h"
@class STTweetLabel;
@interface STViewController : UIViewController <STLinkProtocol>
@interface STViewController : UIViewController
@property (strong, nonatomic) STTweetLabel *tweetLabel;
@property (strong, nonatomic) IBOutlet UILabel *displayLabel;

View File

@@ -7,7 +7,6 @@
//
#import "STViewController.h"
#import "STTweetLabel.h"
@interface STViewController ()
@@ -33,9 +32,36 @@
[_tweetLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17.0]];
[_tweetLabel setTextColor:[UIColor blackColor]];
[_tweetLabel setDelegate:self];
[_tweetLabel setText:@"Hi. This is a new tool for @you! Developed by->@SebThiebaud for #iPhone #ObjC... ;-)\nMy GitHub page: https://t.co/pQXDoiYA"];
STLinkCallbackBlock callbackBlock = ^(STLinkActionType actionType, NSString *link) {
NSString *displayString = NULL;
switch (actionType) {
case STLinkActionTypeAccount:
displayString = [NSString stringWithFormat:@"Twitter account:\n%@", link];
break;
case STLinkActionTypeHashtag:
displayString = [NSString stringWithFormat:@"Twitter hashtag:\n%@", link];
break;
case STLinkActionTypeWebsite:
displayString = [NSString stringWithFormat:@"Website:\n%@", link];
break;
}
[_displayLabel setText:displayString];
};
[_tweetLabel setCallbackBlock:callbackBlock];
[self.view addSubview:_tweetLabel];
}
- (void)didReceiveMemoryWarning