-3

i have to view controllers .. and i want to pass data from the first to the second view i have this code

for the ViewController.h

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

  @interface ViewController : UIViewController{
      SecondView *secondviewData;
      IBOutlet UITextField *textfield;
  }
  @property (nonatomic,retain) SecondView *secondviewData;
  -(IBAction)passdata:(id)sender;


  @end

for the viewController.m

  #import "ViewController.h"
  #import "SecondView.h"

  @interface ViewController ()

  @end

  @implementation ViewController

  @synthesize secondviewData;


  -(IBAction)passdata:(id)sender{
      SecondView *second= [[SecondView alloc] initWithNibName:nil bundle:nil];

      self.secondviewData=second;
      secondviewData.passedValue=textfield.text;

     [self presentModalViewController:second animated:YES];

  }

  - (void)viewDidLoad
  {
      [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
  }

  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }

  @end

for the second view controller which is SecondView.h

  #import <UIKit/UIKit.h>

  @interface SecondView : UIViewController{
      IBOutlet UILabel *label;
      NSString *passedValue;

  }
  @property (nonatomic,retain) NSString *passedValue;
  -(IBAction)back:(id)sender;


  @end

for the SecondView.m

  #import "SecondView.h"
  #import "ViewController.h"

  @interface SecondView ()

  @end

  @implementation SecondView

  @synthesize passedValue;



  -(IBAction)back:(id)sender{
      ViewController *vc= [[ViewController alloc] initWithNibName:nil bundle:nil];
     [self presentModalViewController:vc animated:YES];
  }


  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
    // Custom initialization
      }
      return self;
  }

  - (void)viewDidLoad
  {
      label.text=passedValue;
      [super viewDidLoad];
      // Do any additional setup after loading the view.
  }

  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }

  /*
  #pragma mark - Navigation

  // In a storyboard-based application, you will often want to do a little preparation       before navigation
  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
      // Get the new view controller using [segue destinationViewController].
      // Pass the selected object to the new view controller.
  }
  */

  @end

the error I'm having is presentModalViewController deprecated iOS 6 in these 2 lines

     [self presentModalViewController:vc animated:YES];


     [self presentModalViewController:vc animated:YES];

and when i run it and click on the button it will stop working and displays a blank black page

Wain
  • 118,658
  • 15
  • 128
  • 151
  • So do you have problem with "passing data from view controller to another", or with using deprecated methods? – Kreiri Jun 25 '14 at 10:39
  • @Kreiri the first problem is swapping between the 2 views so i can't tell if the data were passed or not because the second view won't be displayed yet – user3074531 Jun 25 '14 at 10:42
  • your code is very confusing and incoherent. where the elements of the Second Controller are added to the view? the analysis to your code, it is possible to present views controllers inside views controllers. – DaSilva Jun 25 '14 at 10:43
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – E-Riddie Jun 25 '14 at 10:46

2 Answers2

3

You can save data in AppDelegate to access it across view controllers in your application. All you have to do is create a shared instance of AppDelegate

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
iAnurag
  • 9,286
  • 3
  • 31
  • 48
ak_tyagi
  • 1,552
  • 2
  • 12
  • 20
0

If you are building for >= iOS 5 then you should be using presentViewController:animated:completion: instead of presentModalViewController:animated:.

Also, the idea of 'back' doesn't usually mean pushing something new. So, this code:

- (IBAction)back:(id)sender {
    ViewController *vc= [[ViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:vc animated:YES];
}

should change to:

- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Now, you should look at using a delegate pattern to pass the data back to the originating view controller. And preferably that delegate implementation would include telling the delegate that the second controller is finished and the delegate triggers the dismiss.

See Passing Data between View Controllers

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151