在不使用Segue的情况下在ViewController之间传递数据

时间:2015-12-15 05:46:51

标签: ios objective-c uiviewcontroller storyboard

我知道这个问题是重复的,但要求差别不大,所以在这里张贴。我知道如何通过定义属性来保存从第一个ViewController传递的值,从一个ViewController传递值到另一个。我附上ScreenShot以便更好地理解。 enter image description here我所做的是将UIPageViewController嵌入NavigationController(SwipeBetweenViewController)。来自UIPageViewController以编程方式调用UIViewController(ProfileViewController)。单击LOG IN按钮后,获取一些响应,将其存储在变量中。现在我要做的就是将该变量传递给ProfileViewController。我在ProfileViewController.h中定义了property,将imported ProfileViewController.h定义为LoginViewController.m。如果从LoginViewController传递数据,我会直接在ProfileViewControllerUiPageViewController之间传递数据。这是代码,我试过但它不起作用。执行控件保留在同一页面上,没有导航。

ProfileViewController.h

 @interface KKProfileViewController : UIViewController
 @property(copy, nonatomic) NSString *userEmailId;
 @end

LoginViewController.m

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    if (error) {
        // Handle error
    }
    else {
        NSError *tempError;

        NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
        NSString *loginResponse =response[@"message"];
        _emailId =response[@"email"];

        if ([loginResponse isEqualToString:@"Welcome"])

        {
            [self passLoginDataForward];
            [self performSegueWithIdentifier:@"loginSuccess" sender:self];

        }
        else
        {
            //code for error alert

        }

        NSLog(@"Response is :%@", response);

    }

}


-(void)passLoginDataForward
{

    ProfileViewController *viewControllerProfile =[self.storyboard instantiateViewControllerWithIdentifier:@"profileViewController"];

    viewControllerProfile.userEmailId = _emailId;
    NSLog(@"user Email %@", viewControllerProfile.userEmailId);
    [self.navigationController pushViewController:viewControllerProfile animated:YES];
}

SwipeViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
self.navigationBar.translucent = YES;
firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"profileViewController"];
secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"dashboardViewController"];
thirdVC = [self.storyboard instantiateViewControllerWithIdentifier:@"newsViewController"];
viewControllerArray = [[NSMutableArray alloc]init];
viewControllerArray = @[firstVC,secondVC,thirdVC];
self.currentPageIndex = 0;
self.isPageScrollingFlag = NO;
self.hasAppearedFlag = NO;
}
-(void)setupPageViewController 
{
pageController = (UIPageViewController*)self.topViewController;
pageController.delegate = self;
pageController.dataSource = self;
[pageController setViewControllers:@[[viewControllerArray objectAtIndex:0]]    direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
 [self syncScrollView];
  }

3 个答案:

答案 0 :(得分:1)

问题是因为您的登录视图控制器可能没有导航控制器而您正在尝试推送视图控制器。在这种情况下什么都不会发生。

如果要将页面视图控制器推送到登录视图导航堆栈,请将您的登录视图控制器嵌入导航控制器(选择登录视图控制器编辑器> Ember>导航控制器)并添加segue to pageviewcontroller(直接来自登录视图控制器,而不是来自任何按钮)。添加segue的标识符(比如yourSegueID)然后实现以下方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"yourSegueID"]) {
        UIPageViewController *pageViewController = [segue destinationViewController];
        ProfileViewController *viewControllerProfile =[self.storyboard instantiateViewControllerWithIdentifier:@"profileViewController"];

        viewControllerProfile.userEmailId = _emailId;

        [pageViewController setViewControllers:@[viewControllerProfile] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }
}

然后致电

[self performSegueWithIdentifier:@"yourSegueID" sender:nil];

第二个选项

如果你想创建新的导航堆栈,就像在当前的故事板实现中那样,将segue从登录视图控制器导航到导航控制器一个当前的模态segue然后更改prepareForSegue

中的后续行
    UIPageViewController *pageViewController = [segue destinationViewController];

UINavigationController *navController = [segue destinationViewController];
UIPageViewController *pageViewController = navController.viewControllers[0];

<强>更新

根据swipeviewcontroller的新代码进行更新 在这种情况下,您还必须在滑动视图控制器中添加电子邮件属性。然后将其设置为准备segue方法。然后在滑动视图控制器viewdidload中设置配置文件视图控制器属性

答案 1 :(得分:0)

    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
         {
             if (error) 
            {
                // Handle error
            }
            else 
            {
                NSError *tempError;

                NSDictionary* response=(NSDictionary*)[NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&tempError];
                NSString *loginResponse =response[@"message"];
                _emailId =response[@"email"];

            ///////////////////////
            //set your Email in nsuserdefaults

            [NSUserDefaults standardUserDefaults][setObject:_emailId forKey:@"email"];
                [[NSUserDefaults standardUserDefaults]synchronize];

            ///////////////////////


                if ([loginResponse isEqualToString:@"Welcome"])

               {
                   [self passLoginDataForward];

               }
                else
               {
                 //code for error alert

               }

                NSLog(@"Response is :%@", response);

            }

 }


-(void)passLoginDataForward
{

ProfileViewController *viewControllerProfile =[self.storyboard instantiateViewControllerWithIdentifier:@"profileViewController"];

[self.navigationController pushViewController:viewControllerProfile animated:YES];
}

在ProfileViewController.m中获取值

 -(void)viewWillAppear:(BOOL)animated
 {
        [super viewWillAppear:animated];
        userEmailId = [[NSUserDefaults standardUserDefaults]objectForKey:@"email"];

 }

答案 2 :(得分:0)

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
YourViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"identifier"];
vc.something = something;
[self.navigationController pushViewController:vc animated:YES];

使用此代替self.storyboard