Search code examples
iosuisplitviewcontrolleruistoryboardsegueipad

Passing VALUE between 2 View Controllers in SplitView Controller


I have the scenario like this: I'm new to Xcode and have done the samples also. Actually I used SplitView Controller in that everything works fine, but when comes to the detail View controller I mean in the Navigation Cotroller with two ViewControllers.

In that I just used a "Picker" to get location and when I just press get location , It segue to the 2nd View Controller , But here I'm not retrieving the vale. Here is the piece of code

//ViewController1.h

#import "QuerySubmission.h"

@interface MDDetailViewController : UIViewController <UISplitViewControllerDelegate>
{
    IBOutlet UIPickerView *pickerView;
    NSArray *pickerViewArray;
    NSString *setLocation;


}

@property (nonatomic, retain) NSArray *pickerViewArray;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property(nonatomic,retain) NSString *setLocation;

@property (strong, nonatomic) id detailItem;

@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;

- (IBAction)getLocation:(id)sender;

@end

//ViewController1.m

 - (IBAction)getLocation:(id)sender {
    int selectedIndex = [self.pickerView selectedRowInComponent:0];
    NSString *message = [NSString stringWithFormat:@"Selected Location: %@",[pickerViewArray objectAtIndex:selectedIndex]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    QuerySubmission *obj = [[QuerySubmission alloc] initWithNibName:@"QuerySubmission" bundle:nil];
    obj.location=[pickerViewArray objectAtIndex:selectedIndex];

    //NSLog(@"loc %@", [ pickerViewArray objectAtIndex:selectedIndex]);
    //[ pickerViewArray objectAtIndex:selectedIndex];
}

Here I'm getting the location list and retrieving

//ViewController2.h

#import <UIKit/UIKit.h>
#import "MDDetailViewController.h"

@interface QuerySubmission : UIViewController <UISplitViewControllerDelegate>
{
    NSString *location;
}

@property(nonatomic,retain) NSString *location;
- (IBAction)querySubmit:(id)sender;

@end

But as like to move to another Controller. It just prints "null" . I'm new to SplitView Controller. So am I Doing anything wrong, or need to declare anything at "delegates """ ? Please do needful


Solution

  • If you're doing a segue from one controller to the next, passing values can be as easy as adding this bit of code in your ViewController1 implementation:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        QuerySubmission * viewController2 = segue.destinationViewController;
        if(viewController2)
        {
            viewController2.location=[pickerViewArray objectAtIndex:selectedIndex];
        }
    }
    

    And myself, I try to minimize stuffing variables into the app delegate, which really should mostly be used for UIApplicationDelegate type things like the application being suspended, etc.