سؤال

In my TableViewController, I set the data I want to load in my DetailView (ws)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

    detailViewController *detailVC = [[detailViewController alloc] init];
    detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
    [detailVC viewDidLoad] ;


    //detailVC.descriptionTextView = [[UITextView alloc] init];

    //[self.navigationController pushViewController:detailVC animated:YES];
}

And this is what my DetailView must load

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.barre.title = self.ws.associationName ;
    self.descriptionTextView.text = self.ws.associationDescription ;
}

But when I select the row, a white page appears and not my associationDescription in a text view, neither my associationName But my viewDidLoad seems to be called before detailVC.ws is loaded, ie my detailVC is empty

Here is my TableViewController.h

import UIKit/UIKit.h

@interface MasterViewController : UITableViewController{
    NSArray *tabAssociation;
}

@property (nonatomic, retain) NSArray *tabAssociation;


@end

And the TableViewController.m

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {

}
@end

@implementation MasterViewController


@synthesize tabAssociation ;

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSArray *dictFromFile = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"AssociationTest" ofType:@"plist"]];    

    NSMutableArray *associationToAdd = [[NSMutableArray alloc] init];
    NSEnumerator *enumerator = [dictFromFile objectEnumerator];
    NSDictionary *anObject;
    while ((anObject = [enumerator nextObject])) {
    association *newAssocition = [[association alloc] initWithDictionaryFromPlist: anObject];
        [associationToAdd addObject: newAssocition];

    }

    self.tabAssociation = [NSArray arrayWithArray:associationToAdd];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tabAssociation.count;
}

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // On récupère l'objet Website qui correspon à la ligne que l'on souhaite afficher
    association *ws = [self.tabAssociation objectAtIndex:indexPath.row];

    cell.textLabel.text = ws.associationName;

  // On renvoie la cellule configurée pour l'affichage
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

detailViewController *detailVC = [[detailViewController alloc] init];
detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
//[detailVC viewDidLoad] ;
[self presentViewController:detailVC animated:YES completion:nil];


//detailVC.descriptionTextView = [[UITextView alloc] init];

//[self.navigationController pushViewController:detailVC animated:YES];
}

/*- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSDate *object = _objects[indexPath.row];
    [[segue destinationViewController] setDetailItem:object];
    }
}*/

@end
هل كانت مفيدة؟

المحلول

You should never call -viewDidLoad directly. What you want to do is present you detailViewController somehow.

If you're inside a UINavigationController, something like:

[self.navigationController pushViewController:detailVC animated:YES];

should work. If you're not, then you have to think about how you're going to present your view controller.

You can try something like:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath sender:(id)sender{

    detailViewController *detailVC = [[detailViewController alloc] init];
    detailVC.ws = [self.tabAssociation objectAtIndex:indexPath.row];
    [self presentViewController: detailVC animated:YES completion:nil];

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top