无法将文本分配给UITextView

时间:2014-04-05 21:47:36

标签: ios iphone objective-c

我正在尝试创建一个用户打开应用程序的iOS应用程序,只需点击一个单元格即可查看某个主题的信息。我认为所有设置都相应,但是当我运行应用程序并点击一个单元格时,我的.txt文件没有显示在textView中。我做错了什么?

My Xcode project

.m文件的详细视图控制器

@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize detailText = _detailText;

 (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}


-(void)viewDidAppear:(BOOL)animated{

    self.textView.text = _detailText;
}

.h文件的详细视图控制器

 @interface DetailViewController : UIViewController

    @property (nonatomic, retain) UITextView *detailText;
    @property (strong, nonatomic) IBOutlet UITextView *textView;

    @end

*.m file of tableviewcontroller*

@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}



 - (void)viewDidLoad
{
   self.tableViewText = [[UITextView alloc] init];
    [super viewDidLoad];

    self.titleObjectsArray = @[@"D 1.1",
                               @"D 1.2",
                               @"D 1.3",
                               @"D 1.4",];

    self.subtitleObjectsArray = @[@"Pharmaceutical Products",
                               @"Pharmaceutical Products",
                               @"Pharmaceutical Products",
                               @"Pharmaceutical Products",];

    self.textObjectsArray = @[@"D 1.1.txt",
                              @"D 1.2.txt",
                              @"D 1.3.txt",
                              @"D 1.4.txt",];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{


    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return [self.titleObjectsArray count];;
}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.titleObjectsArray objectAtIndex: indexPath.row];
    cell.detailTextLabel.text = [self.subtitleObjectsArray objectAtIndex:indexPath.row];



        return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.tableViewText.text = [self.textObjectsArray objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"detail" sender:self];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"detail"]) {
        DetailViewController *detailVC = (DetailViewController *)segue.destinationViewController;
        detailVC.detailText = self.tableViewText;
    }
}

1 个答案:

答案 0 :(得分:1)

在整个项目的各个地方,您都错过了使用对象时必须采取的重要步骤。将以下代码添加到TableView的viewDidLoad()函数中。

  self.tableViewText = [[UITextView alloc] init];

这应指向正确的方向来开始修复错误。

考虑何时以及为什么需要初始化对象,考虑init方法应该在类中执行的操作,并考虑在各种赋值语句中执行的操作。问问自己,"我想将整个对象分配给某个东西,还是只想分配该对象的一个​​属性?"

这是DetailView中您执行任务错误的示例:

-(void)viewDidAppear:(BOOL)animated{

    self.textView.text = _detailText;
 }

此外,请考虑哪些对象最适合您要完成的任务。是否存在可能存在比UITextView更适合存储文本的对象?

我认为您需要重新审视您用于学习objective-c的资源的一些开头章节。您缺少一些必须了解的基本概念才能完成任何工作。