CoreData:如何将获取的数据从一个View控制器发送到其他视图控制器

时间:2013-11-30 08:53:14

标签: ios

我正在尝试登录申请表,其中用户可以登录并且SignIN.i成功完成将数据保存到coreData.in中。用户必须输入用户名密码。我有一个按钮signIN.when输入用户名后点击signIN按钮和密码,用户可以导航到他的欢迎页面与他的details.i编写登录代码和导航到欢迎页面它工作正常。但是当导航到欢迎页面我得到空白标签没有用户详细信息。这是我的代码< / p>

- (IBAction)sginin:(id)sender {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Userdetails"];
self.userData= [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

UserDetailViewController *userdetals = [[UserDetailViewController alloc] init];

[userdetals.usersDataArray setArray:_userData];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(userName == %@) AND (passWord == %@)", self.userNameField.text, self.userPasswordField.text];

[fetchRequest setPredicate:predicate];

self.userdata = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];


if ([_userdata count] > 0)
{
    UserDetailViewController  *userDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"userpage"];
    [self.navigationController pushViewController:userDetailViewController animated:YES];
    NSLog(@"login success");
}                                                                                               

我在欢迎页面中这样做以显示用户详细信息

  -(void)viewDidAppear:(BOOL)animated{

  [_userFirstName     setText:[_usersDataArray        valueForKey:@"firstName"]];
  [_userLastName      setText:[_usersDataArray        valueForKey:@"lastName"]];
  [_userNameLable     setText:[_usersDataArray        valueForKey:@"userName"]];
  [_password          setText:[_usersDataArray        valueForKey:@"passWord"]];
  [_userAdress        setText:[_usersDataArray        valueForKey:@"adress"]];
  [_userPhone         setText:[_usersDataArray        valueForKey:@"contactNumber"]];
  [_userEmailLable    setText:[_usersDataArray        valueForKey:@"emailAdress"]];

  [self.view setNeedsDisplay];

}    但欢迎页面随附空白详细信息。我在哪里做错了。有人帮助我。

2 个答案:

答案 0 :(得分:0)

你可以在prepareforsegue

上使用它
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.destinationViewController respondsToSelector:@selector(setDetail:)]) {
            [segue.destinationViewController performSelector:@selector(setDetail:) withObject:goalDetail];
    }

如果目标viewcontroller上没有可用的相应方法,则respondsToSelector将为false,因此performSelector将不会被执行。您可以确保您的set方法正常工作。

答案 1 :(得分:0)

这是大多数初学者犯的常见错误。您的UserDetailViewController是否在故事板中?那么为什么要使用alloc创建视图控制器?

UserDetailViewController *userdetals = [[UserDetailViewController alloc] init]; // not required

[userdetals.usersDataArray setArray:_userData]; // setting the usersDataArray on the above created instance

稍后,您从故事板中实例化,再次成为另一个实例,第二个缺少usersDataArray。

 UserDetailViewController  *userDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"userpage"]; // why you not setting array for this?
    [self.navigationController pushViewController:userDetailViewController animated:YES];

最后来解决方案。这就是你所需要的一切

 UserDetailViewController  *userDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"userpage"]; 
[userDetailViewController.usersDataArray setArray:_userData];
        [self.navigationController pushViewController:userDetailViewController animated:YES];