-2

i created a 3 Tab-based app in which the FirstViewController has a Display UILabel property and well, i have the SecondViewController Tab (All in Sotoryboard) which holds Data to display on the FirstViewController but i cant seem to get this to happen without getting an Error. I Tried using delegates but i end up crashing my app.

This is the Number 1 problem i have on my app. Can anyone shed some light to this problem ?

Any examples from a SecondViewController to pass an NSString value from the View itself to the FirstViewController ?

Example:

NSString *valueString; // This String holds a value to display onto the First View Controller when i tap a save Button on the same Second View controller.

in the First View Controller, i have a UILabel named:

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

to which is connected to a UIlabel in Storyboard but cant seem to make the NSString *valueString to display its data onto the displayValue Property Label.

Anyone here has a best way of doing this ? and where would i Put a Delegate if i need to ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amit
  • 310
  • 1
  • 3
  • 15
  • http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/ – Jay Gajjar Oct 18 '13 at 05:03
  • with the use of passing your firstviewcontroller as parent you can do that and with the use of global variable in appDelegate you can do that so there are many ways with which you can achieve this. – D-eptdeveloper Oct 18 '13 at 05:03
  • 1
    check this link http://stackoverflow.com/questions/16208041/what-is-the-use-of-singleton-class-in-objective-c – kirti Chavda Oct 18 '13 at 05:07
  • Using delegation is the way you should do this. If that's not working for you, then you should show how you're trying to do that, so we can help. – rdelmar Oct 18 '13 at 05:27

3 Answers3

0

When you create the second view controller in the first view controller .m file:

secondViewController.delegage = self;

When you click the button in the second view controller .m file:

[delegate.displayValue setText:valueString];

In the .h for the second view controller:

@property (strong, nonatomic) FirsViewController *delegate;
Joel
  • 15,654
  • 5
  • 37
  • 60
0

There are two ways for achieving it. 1.Make a singleton class for shared data(in your case that string which you want to pass from one ViewController to another).

2.Declare shared data in appDelegate class.(you should use second mathod because you just want string to be shared).

In your appDelegate.h declare your string

@property(retain,nonatomic) NSString *YourString;

now in your second viewController assign the value you want.

AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
appdelegate.YourString = @"Your VAlue";

Now access the same string from first ViewController.

 AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];



 self.yourLable.text = appdelegate.YourString;

I couldn't attache source code here.it is very simple you just folow the instructions.

1.Open AppDElegate.h file and paste following code.

@property(strong,nonatomic) NSString *SharedString;

2.Now Open SecondViewController.m and import appDelegate.h

#import "AppDelegate.h"

3.In SeconViewController.m initialize sharedString with your value

ViewDidLoad
{
 AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
    appdelegate.SharedString=@"Any String";

}
  1. Open FirstViewController.m and import appDelegate.h

    #import "AppDelegate.h"

5.In FirstViewController.m paste Following Code

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
    self.YouLable.text = appdelegate.SharedString;
    [super viewDidAppear:NO];

}
Wali Haider
  • 1,202
  • 1
  • 16
  • 23
  • I have a question, where would i add the second line of code which is : AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; appdelegate.YourString = @"Your VAlue"; so where would i place this ? Like in the secondViewController but do i place it in viewDidLoD or in the save button body ? – Amit Oct 21 '13 at 03:37
  • In second viewController you set your string to shared string object i.e. YourString in SecongViewController.m(either in viewDidLoad or IBAction or where ever you want) AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; appdelegate.YourString = @"Your VAlue"; In first viewController's ViewDidLoad assign the same string to your lable or where ever you want. AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; self.yourLable.text = appdelegate.YourString; – Wali Haider Oct 21 '13 at 04:50
  • Do you have any example project you could send me ? Like an example xcode project into which SecondViewController string value is passed to the FirstViewController view ? I Tried every way and yours seems to make sence but somehow it doesnt work for me. either i am missing something in my code or i just dont follow so yeah, Let me know and Big Thnx :) – Amit Oct 21 '13 at 20:30
  • I couldn't attache source code here.it is very simple you just folow the instructions. – Wali Haider Oct 22 '13 at 04:30
  • Could you send me an example project to h.arreola10(at)gmail(dot)com? – Amit Oct 22 '13 at 04:49
  • I have edited my comment check it first – Wali Haider Oct 22 '13 at 05:01
  • i have sent you source code Cheerss!! :) – Wali Haider Oct 22 '13 at 05:09
-2

Try with way...

IN secondViewController

make the object of the FirstView.

FirstView *fc=[[FirstView alloc]initWithNibName:@"FirstView" bundle: nil];
fc.displayValue.text= ValueString;
user1673099
  • 3,293
  • 7
  • 26
  • 57
  • 1
    This is wrong -- it creates a new instance of FirstViewController instead of referencing the one he already has that's one of the tab bar controller's view controllers. – rdelmar Oct 18 '13 at 05:24