-2

I'm trying to make an iOS application where the user opens the app, and simply taps on a cell to view information on a certain topic. I have everything set up accordingly I think, however when I run the app and tap on a cell, my .txt file is not showing up in the textView. What am I doing wrong?

My Xcode project

.m file of detail view controller

@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 file of detail view controller

 @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;
    }
}
  • 2
    Welcome to SO. You should include what you think the relevant piece of code is in your question itself rather than expect people to work through your entire project. Have you used the debugger to single-step through your code - for example `didSelectRowAtIndexPath` ? Have you used NSLog messages to see what is happening? Have you tried setting simple text directly into your textView to make sure that your controls are linked up properly? – Paulw11 Apr 05 '14 at 21:57
  • @Paulw11 Well, there is an Semantic issue in my view controller.h file, ' self.textView.text = _detailText;'. I'm not really sure why the text isn't showing. – user3502282 Apr 05 '14 at 22:19
  • You can't put code like `self.textView.text = _detailText;` in your .h file - this needs to go in methods in your .m file. You also need to inspect things with the debugger. Set a breakpoint where you are trying to set the text and then inspect things - is self.textView nil? for example - does _detailText contain what you expect? – Paulw11 Apr 05 '14 at 22:38
  • @Paulw11 My mistake, it was in the .m file. That line's issue is "Incompatible pointer types assigning to 'NSString *' from 'UITextView *". I put a breakpoint where the app is supposed to show the .txt file, yet nothing came up... This is the .m file minus some code. `@interface DetailViewController () @end @implementation DetailViewController @synthesize detailText = _detailText; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } -(void)viewDidAppear:(BOOL)animated{ self.textView.text = _detailText; }` – user3502282 Apr 05 '14 at 22:54
  • Please edit your question as it is hard to read code in a comment. What have you initialised _detailText to? – Paulw11 Apr 05 '14 at 23:05
  • @Paulw11 so, i have a TableViewController.h and .m and a detail view controller. h and .m. The table view controller is for the main view where it shows the table and its cells. The detailView is to show the textView on another controller. – user3502282 Apr 06 '14 at 03:17
  • -3 votes ! what a great welcome greetings for a new friend. Lucky you had @Paulw11 to help you. Can't you guys just hold it form your passion for down votes ? for a new user it will be fine with -1 and a great guy to help him out with his first question. thanks – shannoga Apr 06 '14 at 04:32
  • BTW it is -2 as I up voted. – shannoga Apr 06 '14 at 04:34

1 Answers1

1

In various places throughout your project you're missing an important step you must take when using objects. Add the following code to the viewDidLoad() function of your TableView.

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

This should point in the right direction to begin fixing your errors.

Think about when and why you need to init an object, think about what the init method should do in your classes, and also think about what you are doing in your various assignment statements. Ask yourself, "do I want to assign entire an entire object to something, or do I want to assign just one property of the object?"

This is an example in DetailView of where you're doing assignment wrong:

-(void)viewDidAppear:(BOOL)animated{

    self.textView.text = _detailText;
 }

Further, think about what objects would be best suited for what you are trying to accomplish. Are there places where there may be an object better suited for storing text than UITextView?

I think you need to revisit some of the beginning chapters of whichever resource you used to learn objective-c. You are missing some basic concepts that you must understand in order to get anything accomplished.

CocoaDog
  • 305
  • 1
  • 11
  • I added your line of code under the viewDidLoad, however when I tap a cell, it brings me to the line, `return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));` and the information from the .txt still doesn't show. I edited my question to show the edited line of viewDidLoad. And yeah I really need to take some course again to make sure I get the complete idea of what I'm really doing. – user3502282 Apr 06 '14 at 04:17
  • That's what I mean about checking on your assignment statements. If you try to assign an object of one type to an object of a different type, you'll get a crash. I updated the answer to include an example. – CocoaDog Apr 06 '14 at 04:26
  • Is there a problem because I used an equal pointer? – user3502282 Apr 06 '14 at 04:55
  • No. You are trying to assign _detailText, a UITextView, to self.textview.text, an NSString. Those two types cannot be assigned to each other. – CocoaDog Apr 06 '14 at 05:28
  • Unfortunately, this isn't the place to get your hand held through basic programming concepts or to have someone write your code for you. As I stated in the answer, your app is having trouble because you aren't initializing objects correctly and you aren't using assignment correctly. You also aren't passing text data around correctly thus causing more confusion for yourself. Research those topics and the NSString data type and you'll be able to easily spot your errors. – CocoaDog Apr 06 '14 at 05:37