0

this is the code for.m class of first viewcontroller[][1]I had tried accessing value of IVAR of one viewcontroller to another viewcontroller by making use of property , but i am getting null value.

please let me know how to get value displayd in my secondviewcontroller.

enter image description here

6
  • Show your code. Commented Oct 28, 2017 at 10:51
  • see the pictures that i had attached Commented Oct 28, 2017 at 11:13
  • Your code is creating a new instance of the view controller (in which the property value is nil) rather than accessing the existing instance. Commented Oct 28, 2017 at 11:16
  • Have a read of this question and the numerous answers. They explain very well how to pass data between VCs. Commented Oct 28, 2017 at 11:20
  • Possible duplicate of Passing Data between View Controllers Commented Oct 28, 2017 at 11:21

1 Answer 1

1

You are doing the right way to get value of property. But you need to make sure myString has been assigned a value in init method of ViewController.

Try in ViewController.m:

- (instancetype)init {
    self = [super init];

    if (self) {
        self.myString = @"My String";
    }

    return self;
}

Beside of it, make sure that myMetod of SecondViewController is called. If myMetod is called, "Print value My String" will be printed.

Sign up to request clarification or add additional context in comments.

4 Comments

i had puted (instance)init on top of viewdidload method of viewcontroller and put code in it , but when i run app after calling of viewdidload nothing happens .. even init method is not calling
do i need to call init method too in view did load
You need to make sure that myMetod of SecondViewController is called. I don't see where you call myMetod
Thanks alot i got it

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.