认为没有传递价值观

时间:2013-08-22 04:39:44

标签: ios objective-c xcode

我在尝试将联系人(loggedinUser)的信息传递给destinationViewControllers的联系人(currentUser)时创建了一个segue,但由于某种原因它没有通过。下面的代码有问题吗?

原始观点:

...
self.loggedInUser.firstName = @"first name";
self.loggedInUser.lastName = @"last name";
self.loggedInUser.company = @"company"
self.loggedInUser.position = @"position";
[self performSegueWithIdentifier:@"profileSegue" sender:self];
...

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ProfileViewController *pvc = (ProfileViewController *) [segue destinationViewController];
pvc.currentUser = self.loggedInUser; }

然后我在标签中显示值 DestinationViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *fullName = [NSString stringWithFormat:@"%@ %@", currentUser.firstName, currentUser.lastName];
    fullNameLabel.text = fullName;
    companyLabel.text = currentUser.company;
    positionLabel.text = currentUser.position;
}

loggedInUser和currentUser都是“Contact”类型。但标签最终显示为null。我错过了一些明显的东西吗?

修改

self.loggedinUser无法传递给prepareForSegue。

标题代码:

#import "Contact.h"

@interface LoginViewController : UIViewController
{
}

...

@property (nonatomic, retain) Contact *loggedInUser;

1 个答案:

答案 0 :(得分:1)

我认为在viewDidLoad之前调用了prepareFortSegue,即当currentUser仍为nil时。我建议您将代码从viewDidLoad移至viewWillAppear。或者您可以设置断点来检查呼叫顺序。