I've been learning Objective-C by myself for a couple of months and I am currently stuck on this task for quite some time.
I am using storyboard for this project. I have a custom UITextView
(PlaceholderTextView
) implemented in a custom UITableViewCell
(EditableTableViewCell
) which is in WriteViewController
.
This is my code in WriteViewController.h
:
#import <UIKit/UIKit.h>
#import "EditableTableViewCell.h"
#import "PlaceholderTextView.h"
@interface WriteViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextViewDelegate>
- (IBAction)post:(id)sender;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *postButton;
@end
This is my code in WriteViewController.m:
#import "WriteViewController.h"
@interface WriteViewController ()
@property (strong, readwrite, nonatomic) UITableView *tableView;
@end
@implementation WriteViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
EditableTableViewCell *post = [[EditableTableViewCell alloc] init];
post.postField = [[PlaceholderTextView alloc] init];
NSLog(@"after: %lu", (unsigned long)post.postField.text.length);
post.postField.delegate = self;
}
- (void)textViewDidChange:(UITextView *)textView
{
EditableTableViewCell *post = [[EditableTableViewCell alloc] init];
post.postField = [[PlaceholderTextView alloc] init];
NSLog(@"begin: %lu", (unsigned long)post.postField.text.length);
if ([post.postField.text length] != 0) {
[self.postButton setEnabled:YES];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"PublicCell";
static NSString* CellIdentifier2 = @"PostCell";
if(indexPath.row==0){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier1];
}
return cell;
}
if(indexPath.row==1){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier2];
}
return cell;
}
else{
return 0;
}
}
- (IBAction)post:(id)sender
{
EditableTableViewCell *post = [[EditableTableViewCell alloc] init];
post.postField = [[PlaceholderTextView alloc] init];
NSLog(@"Click: %@",post.postField.text);
}
@end
After compiling and running, I print a few NSLogs when interacting with the UITextView
and the "Post" button to see where I am getting in my project. This is what I get after typing in the UITextView
and clicking the "Post" button. It seems that post.postField.text
is not being registered and keeps printing 0.
2014-05-02 16:47:27.906 PostGap [19024:60b] begin: 0
2014-05-02 16:47:28.107 PostGap [19024:60b] begin: 0
2014-05-02 16:47:28.173 PostGap [19024:60b] begin: 0
2014-05-02 16:47:31.598 PostGap [19024:60b] Click:
2014-05-02 16:47:33.786 PostGap [19024:60b] Click:
2014-05-02 16:47:41.552 PostGap [19024:60b] Click:
Here is the EditableTableViewCell.h:
#import <UIKit/UIKit.h>
#import "PlaceholderTextView.h"
@interface EditableTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet PlaceholderTextView *postField;
@end
And here is the PlaceholderTextView.h:
#import <UIKit/UIKit.h>
@interface PlaceholderTextView : UITextView
@property (nonatomic, copy) NSString *placeholderText;
@end
All I want to do is get the NSLog
to print what I am typing in the UITextView
after clicking the "Post" button. I am getting stumped by this big time. Maybe it is due to my lack of understanding and experience in Objective-C.