forked from cocoaplayground/SALQuickTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSALViewController.m
More file actions
120 lines (93 loc) · 4.69 KB
/
Copy pathSALViewController.m
File metadata and controls
120 lines (93 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// SALViewController.m
// SALQuickTutorialExample
//
// Created by Natan Rolnik on 8/12/14.
// Copyright (c) 2014 Seeking Alpha. All rights reserved.
//
#import "SALViewController.h"
#import "SALQuickTutorialViewController.h"
#import <MZFormSheetController/MZFormSheetController.h>
static NSString *const SALProvidedBySAQuickTutorialKey = @"SALProvidedBySAQuickTutorialKey";
@interface SALViewController ()
@property (weak, nonatomic) IBOutlet UITextField *titleTextField;
@property (weak, nonatomic) IBOutlet UITextField *messageTextField;
@property (weak, nonatomic) IBOutlet UITextField *dismissTextField;
@property (weak, nonatomic) IBOutlet UITextField *uniqueKeyTextField;
@property (weak, nonatomic) IBOutlet UISegmentedControl *completionSegmentedControl;
@property (weak, nonatomic) IBOutlet UISegmentedControl *dismissSegmentedControl;
- (IBAction)showQuickTutorial:(id)sender;
- (IBAction)showHardCodedTutorial:(id)sender;
@end
@implementation SALViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.titleTextField becomeFirstResponder];
}
- (IBAction)showQuickTutorial:(id)sender
{
if (![self validateTextFields]) {
[[[UIAlertView alloc] initWithTitle:@"All text fields need to be filled" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
return;
}
[self showIfNeededForKey:self.uniqueKeyTextField.text title:self.titleTextField.text message:self.messageTextField.text image:[UIImage imageNamed:@"QuickTutorialExampleImage"] dismiss:[self.dismissTextField text]];
}
- (IBAction)showHardCodedTutorial:(id)sender
{
[self showIfNeededForKey:SALProvidedBySAQuickTutorialKey title:@"SALQuickTutorialViewController" message:@"Provided by the Seeking Alpha iOS team. Enjoy! Pull requests are welcome" image:[UIImage imageNamed:@"QuickTutorialExampleImage"] dismiss:nil];
}
- (void)showIfNeededForKey:(NSString *)uniqueKey title:(NSString *)title message:(NSString *)message image:(UIImage *)image dismiss:(NSString *)dismiss
{
[self.view endEditing:YES];
BOOL needsToShow;
if (self.dismissSegmentedControl.selectedSegmentIndex == 0 && self.completionSegmentedControl.selectedSegmentIndex == 0) {
//if you want to enable tapping on background, just use the convenience method
needsToShow = [SALQuickTutorialViewController showIfNeededForKey:uniqueKey title:title message:message image:image];
}
else {
//if you want more customization, like completion block, transition, or setting to dismiss with the button, do it "manually"
needsToShow = [SALQuickTutorialViewController needsToShowForKey:uniqueKey];
if (needsToShow) {
SALQuickTutorialViewController *quickTutorialViewController = [[SALQuickTutorialViewController alloc] initWithKey:uniqueKey title:title message:message image:image];
if ([dismiss length])
{
quickTutorialViewController = [[SALQuickTutorialViewController alloc] initWithKey:uniqueKey title:title message:message image:image dismiss:dismiss];
}
if (self.dismissSegmentedControl.selectedSegmentIndex == 1) {
quickTutorialViewController.dismissesWithButton = YES;
}
if (self.completionSegmentedControl.selectedSegmentIndex == 1) {
[quickTutorialViewController setDidDismissCompletionHandler:^{
[[[UIAlertView alloc] initWithTitle:@"SALQuickTutorialViewController supports completion block" message:[NSString stringWithFormat:@"Quick tutorial with key %@ was dismissed", uniqueKey] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}];
}
[quickTutorialViewController show];
}
}
if (!needsToShow) {
[self alreadyShownWithUniqueKey:SALProvidedBySAQuickTutorialKey];
}
}
- (void)alreadyShownWithUniqueKey:(NSString *)uniqueKey
{
[[[UIAlertView alloc] initWithTitle:@"SALQuickTutorialViewController shows only once per key!" message:[NSString stringWithFormat:@"The key %@ was already used before", uniqueKey] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
}
- (BOOL)validateTextFields
{
for (UITextField *textField in @[self.titleTextField, self.uniqueKeyTextField]) {
if ([textField.text length] == 0) {
return NO;
}
}
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(![touch.view isMemberOfClass:[UITextField class]]) {
[touch.view endEditing:YES];
}
}
@end