0

i want to use one data string stored in extern string variable in one view controller, in the other view by calling that variable in second view controller. and want to print the variable's value in the text box present in second view controller on a button click of button present in it. please tell me how i can do that using objective-C?

please tell what to do in all the 4 files that are: ViewController.h, ViewController.m, SecondViewController.h and SecondViewController.m.

7
  • your questions is not clear Commented Sep 29, 2015 at 9:47
  • yes give a clear idea You need to pass information from vc1 to vc2 ?? Commented Sep 29, 2015 at 9:49
  • yes i need to pass the value entered by in view controller1 to the view controller2. and want to print it in the text box in view controller2 on the button press. Commented Sep 29, 2015 at 9:58
  • how you are pushing view 1 to view 2 can you put your code? Commented Sep 29, 2015 at 10:40
  • this is the code of my ViewController.m file: - (IBAction)buttonPressed:(UIButton *)sender { self.titleLabel.text = self.textField.text; [self.textField resignFirstResponder]; NSString *var = self.textField.text; } Commented Sep 29, 2015 at 10:47

3 Answers 3

1

viewController.m

-(void)viewWillDisappear {

     //pass value to secondViewController
    secondViewController *vc=[[secondviewcontroller alloc]init];
    vc.headStr=@"your string"; //same for array and dictionary
    //Push code.........
 } 

//secondViewController.h

@property (strong, nonatomic) NSString *headStr;


secondViewController.m

-(void)ViewDidLoad
{
[super:ViewDidLoad];
Nslog("%@",self.headStr);
}
Sign up to request clarification or add additional context in comments.

4 Comments

it is printing "null". please give me some solution, thank you.
NearByController *nearByController = [self.storyboard instantiateViewControllerWithIdentifier:@"nearby"]; nearByController.self.headStr=catName; nearByController.self.catId=catId; [self.navigationController pushViewController:nearByController animated:YES];
i am unable to understand what you have done here. please make it simpler for me to do as i am new to IOS. thank you.
NearByController -is Second controller and making its object with its identifier Identifier- we have to set it in storyboard and use same in code for push to that controller nearByController.self.headStr- accessing second contrioller string with its object and passing value to get in next controller
0

if i see your comment on Shameerjans post i see that you dont know what your doing. Please do this tutorial from apple Start Developing iOS Apps

This is a cool jump in and covers very much functionality for displaying elements and using more controllers.

2 Comments

i am new to ios and leaning to build apps.
this is great, the link i send to u is a good jump in. Please read everything carefully and when you dont understand what is written there, research it. Its important that you understand what they mean. Than go to the next chappter. :)
0

I think you need to pass one variable data into another view controller.

viewController.m

 -(void)viewWillDisappear {

     //pass value to secondViewController
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; // use your storyboard name
    secondviewcontroller *vc = [storyboard instantiateViewControllerWithIdentifier:@"secondviewcontroller"];

      vc.value2 = @"String here"; 
 }   

secondViewController.h

 @property (nonatomic, retain) NSString *value2;

secondViewController.m

 @implementation coachViewController
 @synthesize value2;
-(IBAction)buttonPressed {

    self.label.text = value2; //Declare value2 in .h file 
}

9 Comments

yes i want to pass data from one view controller to another view controller , but this code is also not working , it is giving error " undeclared identifier secondViewController" in viewController.m file.
import secondViewController.h in your viewcontroller
it is giving "null" in secondViewController , means by using the above given method the value of value1 is not getting stored in value2. please help , thank you.
How do you change view controller to secondviewcontroller?
Also can you post your code- secondViewController.h and .m ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.