Kunal Kumar
Kunal Kumar

Reputation: 1722

Passing Data between ViewController without using Segue

I know the question is repeated, but requirement is little different so posting here. I know how to pass value from one ViewController to other by defining property to hold the value passed from first ViewController. I am attaching the ScreenShot for better understanding. enter image description here What I did is embed a UIPageViewControllerinto NavigationController(SwipeBetweenViewController). From UIPageViewController calling UIViewController(ProfileViewController) programmatically. After clicking LOG IN button, getting some response, storing it in a variable. Now what I have to do is pass that variable to ProfileViewController.I have defined a property in ProfileViewController.h, imported ProfileViewController.h into LoginViewController.m. I am passing data directly between LoginViewController to ProfileViewController, should it be passed from UiPageViewController. Here is the code, I have tried but its not working. Execution control remains on the same page, no navigation.

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

Upvotes: 0

Views: 474

Answers (3)

Johnykutty
Johnykutty

Reputation: 12829

The problem is because your login view controller may not have a navigation controller and you are trying to push view controller. Nothing will happen in this case.

If you want to push the page view controller to login views navigation stack, embed your login view controller in a navigation controller(Select login view controller Editor>Ember>Navigation controller) And add a segue to pageviewcontroller(directly from login view controller, not from any button). Add an identifier for the segue(say yourSegueID) Then implement the following method

- (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];
    }
}

Then call

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

Second option

If you want to create new navigation stack, as in your current storyboard implementation, make the segue from login view controller to navigation controller a present modal segue then change following line in prepareForSegue

    UIPageViewController *pageViewController = [segue destinationViewController];

to

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

Update

Updating as per your new code for swipeviewcontroller In this case, You have to add email property in swipe view controller too. Then set it in prepare for segue method. Then set profile view controllers property in the swipe view controller viewdidload

Upvotes: 1

Avanish Shrestha
Avanish Shrestha

Reputation: 73

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

Use this instead of self.storyboard.

Upvotes: 0

Nikunj5294
Nikunj5294

Reputation: 391

    -(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];
}

Get Value in ProfileViewController.m

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

 }

Upvotes: 0

Related Questions