0

I have a navigation based application. when I click on a table's row next view appears and back button (manually created )appears. Now i want to know how can I get the object of previous view in current view so that I can change one of the label's text of previous view using previous view's object ?

Thanks.

3 Answers 3

1

You can pass the value before pushing the second view to the necessary variable

secondViewController.variable = firstViewController.variable;

Then push the secondViewController

Update: Use a Bool variable willBePoppedBack as class variable and set it as NO initially.

- (void)viewWillAppear {

    if(willBePoppedBack)
    {
    // your label text after pressing back button
    }
    else {
    // your label's default text
    }
}

You should set the variable willBePoppedBack to YES when you push to the next view.

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

5 Comments

I want to access the first view's object into second view when I click on my custom back button.
you can do this in the viewWillAppear of the first view.
I tried as u said but the change is reflected at the time of it first time load. n what I want to do is when I click back button of the second view then n then only change should be appears.
u can check with a bool variable and then display it only when its appeared from the back button
Done by doing this : get the object from the stack and can access it : RootViewController rootView = (RootViewController)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2]; rootView.value = @"BackFromSecondView"; [self.navigationController popViewControllerAnimated:YES ];
0

If you want to do this you need to make a object of previous view here and then by . operator you can access first page label and write some thing on it.

FirstView *obj=[[[FirstView allo] initWithNibName:@"FirstView"] autorelease];
obj.label.text=@"Text";

Edit:

you need to make a NSString property in appDelegate class then make the object for app delegate and set the value of this string which you want on label now pop the view and in viewWillAppear use this string to write on the label.

It must work.

3 Comments

I dont think this will work coz when u do this a new instance is created, but the necessary change wont be reflected when u pop from the navigation stack as the view controller is not the new one but the one in the stack
is there any way to get the object form the stack ?
get the object from the stack and access it : RootViewController rootView = (RootViewController)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-2]; rootView.value = @"BackFromSecondView"; [self.navigationController popViewControllerAnimated:YES ];
0

Initialize the second view by passing the tableviews object, check the below link, with the similar approach you can achieve your requirement.

navigation based

Comments

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.