我在尝试将联系人(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;
答案 0 :(得分:1)
我认为在viewDidLoad
之前调用了prepareFortSegue
,即当currentUser
仍为nil
时。我建议您将代码从viewDidLoad
移至viewWillAppear
。或者您可以设置断点来检查呼叫顺序。