-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostQuestionsViewController.m
112 lines (90 loc) · 3.55 KB
/
PostQuestionsViewController.m
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
//
// PostQuestionsViewController.m
// Represent
//
// Created by Ria Vora on 7/16/20.
// Copyright © 2020 Ria Vora. All rights reserved.
//
#import "PostQuestionsViewController.h"
@interface PostQuestionsViewController () <UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *questionField;
@property (weak, nonatomic) IBOutlet UIButton *representativeButton;
@property (weak, nonatomic) IBOutlet UITableView *tableViewRepresentatives;
@property (strong, nonatomic) NSArray *representatives;
@end
@implementation PostQuestionsViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupTableView];
self.questionField.delegate = self;
[self setupValues];
}
#pragma mark - Setup
- (void)setupTableView {
self.tableViewRepresentatives.delegate = self;
self.tableViewRepresentatives.dataSource = self;
self.tableViewRepresentatives.hidden = YES;
self.tableViewRepresentatives.tableFooterView = [[UIView alloc]
initWithFrame:CGRectZero];
}
- (void)setupValues {
self.questionField.text = @"What do you want to ask?";
self.questionField.textColor = UIColor.lightGrayColor;
self.representatives = [User currentUser].followedRepresentatives;
[self.currentRepresentative fetch];
[self.representativeButton setTitle:[self.currentRepresentative fullTitleRepresentative] forState:UIControlStateNormal];
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if ([textView.textColor isEqual: UIColor.lightGrayColor]) {
textView.text = @"";
textView.textColor = UIColor.blackColor;
}
return true;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([textView.text isEqualToString:@""]) {
textView.text = @"What do you want to ask?";
textView.textColor = UIColor.lightGrayColor;
}
}
#pragma mark - Actions
- (IBAction)pressedCancel:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
- (NSString *)pressedPost {
NSString *questionText = self.questionField.text;
self.questionField.text = @"";
BOOL questionExists = [Utils checkExists: questionText:@"Question" :self];
if (questionExists) {
return questionText;
} else {
return nil;
}
}
- (IBAction)pressedRepresentative:(id)sender {
self.tableViewRepresentatives.hidden = !(self.tableViewRepresentatives.hidden);
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RepresentativeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RepresentativeCell"];
User *representative = self.representatives[indexPath.row];
[representative fetch];
if (cell == nil) {
cell = [[RepresentativeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RepresentativeCell"];
}
cell.representative = representative;
[cell updateValues];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.representatives.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
RepresentativeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.representativeButton setTitle:[cell.representative fullTitleRepresentative] forState:UIControlStateNormal];
self.currentRepresentative = cell.representative;
tableView.hidden = YES;
}
@end