Shrikant Panchal Shrikant Panchal - 29 days ago 6
Objective-C Question

What are the methods to pass forward data from one View Controller to another using button action?

I'm just a beginner in iOS Devp and stuck with this problem since last 2days. What I am trying to do is I've two view controllers, the first View Controller consists of 2 buttons and I want to load a specific type of data in next View controller on respective button action.

What I did is as follows :
I've two ViewControllers connected using segue with id "showDetailSegue",
1. ViewController
2. SecondVC
I want to update label on SecondVC when the button on ViewController is tapped.

//ViewController.h
#import <UIKit/UIKit.h>
#import "SecondVC.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end


//ViewController.m
#import "ViewController.h"
#import "SecondVC.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
NSString *str = @"my string data..";
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showDetailSegue"]){
SecondVC *controller = segue.destinationViewController;
controller.getString = str;
controller.testLabel.text = str;
}
}
@end

//SecondVC.h
#import <UIKit/UIKit.h>
@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) NSString *getString;
@end

//SecondVC.m


Please help me with a straightforward and clear explanation.

Thanks in advance.!

Answer

There are many ways to pass data to desired ViewController.

1. Using segue

Let suppose you have to pass a string to another VC.

#import "ViewController.h"
#import "SecondVC.h"

@interface ViewController ()
// all instance global variable should be declare here 
NSString str;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    str = @"my string data..";
}

// segue delegate method 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"segueIdentifireName"]) {

        SecondVC *destViewController = segue.destinationViewController;
        destViewController.getString = str;
    }
}
@end

Now you must have to create NSString object inside destinationVc .h file like below.

@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (nonatomic, strong) NSString *getString;
@end

Inside .m file get string data like:

@interface ViewController ()

@end

@implementation SecondVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.testLabel.text = getString;   // we passed `str` data inside `getString` object so it can be refelect here using `getString` variable.
    NSLog(@"string data %@", getString);
}

2. Using storyboard id:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewControllerClassName *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewContIdentifire"];
vc.getString = str;
[self.navigationController pushViewController:vc animated:YES];

Inside .h file:

@property (nonatomic, strong) NSString *getString;

Inside .m file get string data like:

NSLog(@"string data %@", getString);

3. Using NSUserDefaluts (Not recommended).

4. Using local DB.

Comments