1

当被 UINavigationController 包围时,我无法将对象传递到另一个视图。例如,当我使用以下代码时,一切正常,第一个视图的 currentUser 被传递给第二个视图。

    ProfileViewController *pvc = [storyboard instantiateViewControllerWithIdentifier:@"profileVC"];
    pvc.currentUser = currentUser;

    [self.drawerController setCenterViewController:pvc];

但是当我将 pvc 封装在 UINavigationController 中时,currentUser 不会被传递,如下所示。导航控制器是否以某种方式覆盖/创建 ProfileViewController 的新实例?

    ProfileViewController *pvc = [storyboard instantiateViewControllerWithIdentifier:@"profileVC"];
    pvc.currentUser = currentUser;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:pvc];

    [self.drawerController setCenterViewController:navigationController];

(顺便说一句,drawerController来自MMDrawer,方法基本上将指定视图设置为中心)。

编辑(用户声明):

Contact *currentUser = [Contact fetchContactWithLinkedinAccount:[[NSUserDefaults standardUserDefaults] stringForKey:@"linkedinAccount"] inManagedObjectContext:delegate.managedObjectContext];

在 Contact.h 中:

+(Contact *)fetchContactWithLinkedinAccount:(NSString *)account inManagedObjectContext:(NSManagedObjectContext *)context {

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"linkedinAccount == %@", account];
[fetchRequest setPredicate:predicate];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

Contact *fetchedContact;
for(Contact *contact in fetchedObjects) {
    fetchedContact = contact;
}

if(fetchedContact != nil) {
    return fetchedContact;
} else {
    return nil;
}

}

4

1 回答 1

0

检查您的currentUser财产是否像这样声明:

@interface ProfileViewController : UIViewController

@property (nonatomic, strong) Contact * currentUser;

@end
于 2013-08-30T14:05:02.900 回答