ios - Segue 不传递值

标签 ios objective-c xcode

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

原始 View :

...
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; }

然后我在标签中显示值 目标 View Controller :

- (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 都是“联系人”类型。但标签最终显示为空。我是否遗漏了一些明显的东西?

编辑

这是 self.loggedinUser 未传递到prepareForSegue 的问题。

标题代码:

#import "Contact.h"

@interface LoginViewController : UIViewController
{
}

...

@property (nonatomic, retain) Contact *loggedInUser;

最佳答案

我假设viewDidLoadprepareFortSegue之前被调用,即当currentUser仍然是nil时。我建议将您的代码从 viewDidLoad 移至 viewWillAppear。或者您可以设置断点来检查调用顺序。

关于ios - Segue 不传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371808/

相关文章:

ios - 从 Apple Watch 屏幕捕获视频

objective-c - Xcode 4.5 中数组的新语法?

ios - 尝试通过快速桥接头调用扩展方法时发送到实例的无法识别的选择器

重新启动后未保留 Xcode 9 字体和颜色首选项

ios - 为什么 UIBarButtonItem 显示为已禁用?

Objective-C:forKey 和 forKeyPath 有什么区别?

ios - 更改单元格高度后 UITableView 在滚动时跳动

objective-c - Objective c 不喜欢我的 unichars?

iOS 如何最好地将用户对象传递到多个 View Controller (依赖注入(inject))

iphone - 如何在xcode上创建电子邮件表单?