视图控制器中的自定义uitextview类为nil

时间:2014-05-02 09:55:40

标签: ios objective-c uitableview uitextview

我自己一直在学习Objective-C几个月,而且我现在已经坚持了很长一段时间。

我正在为这个项目使用故事板。我在UITextView中的自定义PlaceholderTextViewUITableViewCell)中实施了自定义EditableTableViewCellWriteViewController)。

这是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

这是我在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

编译并运行后,我在与UITextView和&#34; Post&#34;进行交互时打印了几个NSLog。按钮,看看我在项目中的位置。这是我在输入UITextView并点击&#34; Post&#34;按钮。似乎post.postField.text未被注册并且保持打印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:

这是EditableTableViewCell.h:

#import <UIKit/UIKit.h>
#import "PlaceholderTextView.h"

@interface EditableTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet PlaceholderTextView *postField;

@end

这是PlaceholderTextView.h:

#import <UIKit/UIKit.h>

@interface PlaceholderTextView : UITextView

@property (nonatomic, copy) NSString *placeholderText;

@end

我想要做的就是让NSLog在点击&#34;发布&#34;之后打印我在UITextView中输入的内容。按钮。我被这个重要的时刻所困扰。也许是因为我对Objective-C缺乏了解和经验。

2 个答案:

答案 0 :(得分:0)

像这样声明你的细胞:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier1 = @"PublicCell";
     static NSString* CellIdentifier2 = @"PostCell";


   if(indexPath.row==0){

        EditableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[EditableTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                          reuseIdentifier:CellIdentifier1];
            cell.postField.delegate = self;
        }
        return cell;

并删除viewDidLoad中的所有代码..

同样改变这个:

    - (void)textViewDidChange:(UITextView *)textView
{

    NSLog(@"begin: %lu", (unsigned long)textView.text.length);
    if ([textView.text length] != 0) {
        [self.postButton setEnabled:YES];
    }

}

我没有测试过这个,但我认为这应该可行, 祝你好运,

答案 1 :(得分:0)

要打印登录 - (IBAction)帖子:(id)发件人

- (IBAction)post:(id)sender
{
    UIButton *button = (UIButton *)sender;
    CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];
    EditableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
    NSLog(@"postField = %@", cell.postField.text);
}