在不同的viewController之间传递数据后,无法在DetailViewController中设置标签文本

时间:2012-11-08 13:46:00

标签: ios uitableview uiviewcontroller uilabel

将数据从FirstViewController传递给DetailViewController。我无法在DetailViewController中设置标签文本; FirstViewController是一个tableview,很好。

我使用方法updateRowNumber来设置rowNumber。在DetailViewController中,我可以使用调试器来查看rowNumber是否正确。但标签的文字没有显示在视图上。

任何人都可以帮助我吗?

在我的FirstViewController中

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

if (dvController == nil)
{
    DetailViewController *aController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    self.dvController = aController;

    [aController release];
}   

[[self navigationController] pushViewController:dvController animated:YES];
[dvController updateRowNumber:indexPath.row];


}
在我的DetailViewController.h中

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
{
    int rowNumber;
    IBOutlet UILabel *message;
}

@property(readwrite) int rowNumber;
@property(nonatomic, retain) IBOutlet UILabel *message;
- (void) updateRowNumber:(int) theindex;


@end

在我的DetailViewController.m

#import "DetailViewController.h"


@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize message, rowNumber;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void) updateRowNumber: (int) theindex
{
    rowNumber = theindex + 1;
    message.text = [NSString stringWithFormat:@"row %i was clicked", rowNumber];
}

- (void)dealloc
{
    [message release];
    [super dealloc];
}

- (void)viewDidLoad
{
    message.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    //message.text = [NSString stringWithFormat:@"row %i was clicked ", rowNumber];

    [super viewWillAppear: animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
}


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

@end

1 个答案:

答案 0 :(得分:1)

您需要了解视图的加载方式,此过程已在documentation

中详细说明

在加载view之前,nil和所有出口都是view会发生什么情况,您可以在配置之前通过调用self.view;确保加载updateRowNumber:出口[super viewDidLoad]


另请注意,您最好在被覆盖的viewDidLoad的开头致电UIViewContorller,这是有道理的,因为您需要让dealloc人员去做在执行某些自定义逻辑之前,NSObject -dealloc是不同的,因为您需要在标准{{1}}触发之前执行逻辑。希望它是可以理解的。