-3

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.!

Shreekant
  • 297
  • 2
  • 3
  • 10
  • 3
    Why it take 2 days to you, there are a lot of solution for this you just need to google it, check this http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers also this http://stackoverflow.com/questions/12652312/passing-data-between-views – Rajat Nov 03 '16 at 13:30
  • I'm sorry, but I;m just a beginner with iOS devp. Please go through my code and suggest the necessary changes. – Shreekant Nov 04 '16 at 07:09

1 Answers1

1

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);

In Swift3

let controller = UIStoryboard().getControllerInstance(storyBoardName: "MainStoryboard", identifire: "viewContIdentifire")
controller.getString = str;
self.navigationController?.pushViewController(controller, animated: true)

To get data back:

Create protocol where you need to send data back.

// dec var on top
var delegate: YourDelegate!

protocol YourDelegate {
    func delegateFunction(value: String)
}

Call delegate func on tap action:

delegate.delegateFunction(value: "My sample string")

Receiving controller

  • Confirm the delegate to self when navigate
  • Implement the YourDelegate on top.

    func delegateFunction(value: String){ print("Got: ", value) }

3. Using NSUserDefaluts (Not recommended).

4. Using local DB.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • Thank you very much for your time and efforts, but it is still not clear to me. I'm sorry, but I'm just a beginner with iOS devp. Please go through my code and suggest the necessary changes. – – Shreekant Nov 04 '16 at 07:12
  • And I also want to pass the data from one VC to another, fetched originally from a local Data Base. Could you please provide some useful links. – Shreekant Nov 04 '16 at 07:19
  • @ShrikantPanchal check updated ans, now you need to connect segue successfully with `segueIdentifireName` identifier or accordingly. – vaibhav Nov 04 '16 at 07:36
  • @ShrikantPanchal this is an example which tells you how to pass data to another vc you just need to insert and fetch data then pass like above, here are some links to learn db [link 1 using sqlite](http://www.techotopia.com/index.php/An_Example_SQLite_based_iPhone_Application), [link 2 using core data](https://www.techotopia.com/index.php/An_iOS_7_Core_Data_Tutorial). – vaibhav Nov 04 '16 at 07:42
  • Thank you very much ..!! – Shreekant Nov 04 '16 at 13:06