Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 视图控制器中的自定义uitextview类为零_Ios_Objective C_Uitableview_Uitextview - Fatal编程技术网

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

Ios 视图控制器中的自定义uitextview类为零,ios,objective-c,uitableview,uitextview,Ios,Objective C,Uitableview,Uitextview,我自己学习Objective-C已经有几个月了,目前我在这项任务上已经坚持了相当长的一段时间 我正在使用这个项目的故事板。我在WriteViewController中的自定义UITableViewCell EditableTableViewCell中实现了自定义UITextView占位符TextView 这是我在WriteViewController.h中的代码: 编译和运行之后,当与UITextView和Post按钮交互时,我会打印一些NSlog,以查看我在项目中的位置。这是我在UITextV

我自己学习Objective-C已经有几个月了,目前我在这项任务上已经坚持了相当长的一段时间

我正在使用这个项目的故事板。我在WriteViewController中的自定义UITableViewCell EditableTableViewCell中实现了自定义UITextView占位符TextView

这是我在WriteViewController.h中的代码:

编译和运行之后,当与UITextView和Post按钮交互时,我会打印一些NSlog,以查看我在项目中的位置。这是我在UITextView中键入并单击Post按钮后得到的结果。似乎未注册post.postField.text,并继续打印0

以下是可编辑的TableViewCell.h:

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

@interface EditableTableViewCell : UITableViewCell

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

@end
这是占位符文本视图。h:

#import <UIKit/UIKit.h>

@interface PlaceholderTextView : UITextView

@property (nonatomic, copy) NSString *placeholderText;

@end

我只想让NSLog在单击Post按钮后打印我在UITextView中键入的内容。我被这个重大时刻难住了。可能是因为我对Objective-C缺乏了解和经验。

请这样声明您的手机:

    - (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];
    }

}
我还没有测试过这个,但我认为它应该可以工作,
祝你好运,

打印登录-IBActionpost:idsender

- (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);
}

每次要打印文本字段中的内容时,都可以分配并初始化一个新的EditableTableViewCell和Placeholder TextView。您必须获得表视图中的一个。我认为您的问题是,当您执行alloc和init时,您正在实例化一个新的占位符TextView。在我看来,方法是您必须将自定义单元类连接到storyborad及其内部的插座。获取单元格类中的值并从WriteViewController设置一些属性以使其工作后。如果需要静态单元格,可以通过情节提要将EditableTableViewCell和Placeholder TextView直接添加到UITableView中。否则,必须在委托中执行此操作-UITableViewCell*tableView:UITableView*tableView cellForRowAtIndexPath:NSIndexPath*indexPath。因为在您的案例中,您在UITableView中添加了本机UITableViewCell。@BoilingLime,我已将EditableTableViewCell指定给占位符文本视图位于情节提要中的自定义单元格。如何访问EditableTableViewCell.h中完全相同的postField属性并在WriteViewController.m中调用它?我在代码中做得不对吗?谢谢!你真是个天才!不过我还有一个问题。如何在-IBActionpost:idsender中调用cell.postField?@user3595616您能告诉我在该方法中具体要做什么吗?我希望-IBActionpost:idsender打印UITextView的NSLog,该日志在我键入后链接到IBOutlet postField。我希望这能有所帮助。@user3595616并且每个单元格中的按钮都会调用此post方法?或者视图控制器中只有一个按钮?…如果我有10个单元格,当我按下post时,我在5个文本字段中写入内容,它是否应该打印这些文本字段中的5个文本?视图控制器中只有一个按钮,其中第二个单元格中的按钮post按钮调用post方法。视图控制器中只有2个单元格,其中只有第二个单元格具有文本字段。post方法应在单击按钮后打印文本字段。
    - (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;
    - (void)textViewDidChange:(UITextView *)textView
{

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

}
- (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);
}