Failed to pass parentViewController value

I have masterViewController and detailViewController. Masterview pushes detailView. Returning to masteViewController, I want to update the value of foo. But I get NULL from nslog. How to set parenteViewContrller.foo to @ "bar" when going back?

masterViewController.h

NSString *foo; -(void)setFoo:(NSString *)fooValue @property(nonatomic, retain) NSString *foo; 

masterViewController.m

 @synthesize foo; -(void)setFoo:(NSString *)fooValue{ NSLog(@"updated foo:%@", fooValue); } 

detailViewController.m

 -(void)goBack{ [self.navigationController.parentViewController setValue:@"bar" forKey:@"foo"]; [self.navigationController popViewControllerAnimated:YES]; } 
+5
source share
5 answers

[UIViewController parentViewController] will not return what you expect from iOS 5, instead you should use [UIViewController presentingViewController]

If you're just targeting iOS 5, it's enough to just start using presentingViewController , but if I hadn't advised you to manually pass your UIViewController to:

 // DetailViewController.h @class MasterViewController; @interface DetailViewController : UIViewController @property (assign, nonatomic) MasterViewController *masterViewController; @end // DetailViewController.m #import "DetailViewController.h" #import "MasterViewController.h" @implementation DetailViewController - (void)goBack { [self.masterViewController setFoo:@"bar"]; // dismiss this view controller } @end // MasterViewController.h @interface MasterViewController : UIViewController - (void)setFoo:(NSString *)bar; @end // MasterViewController.m #import "MasterViewController.h" #import "DetailViewController.h" @implementation MasterViewController - (void)goForward { DetailViewController *detailViewController = ...; [detailViewController setMasterViewController:self]; // present this view controller } @end 
+7
source

In order for setValue: ForKey: to work, your view controller must conform to the NSKeyValueCoding protocol.

Try the following:

 -(void)goBack{ [self.navigationController.parentViewController setFoo:@"bar"]; [self.navigationController popViewControllerAnimated:YES]; } 

and don't forget to save / free it accordingly in the setFoo: method. You probably also need to type cast parentViewController to avoid any warnings.

0
source

Pass the wizard link with the property assigned to the detail view. Use this link to set the value to foo before performing pop actions.

0
source

gIf you want to pass an object to a parent object, you can try creating an external object in the header file and import the header in the controller where you want to use it, however THIS IS NOT THE BEST PRACTICE, but it is a quick import solution

 extern MyObject* Object; @interface GlobalVariables : NSObject { } @end 

All you have to do is include the header in the controllers and use a global variable.

If you want to do this from books, I suggest you create a delegate that handles the transfer of the object.

0
source

Usually in this scenario you need to use a delegate. See my answer to this similar question in this fooobar.com/questions/901055 / ....

0
source

Source: https://habr.com/ru/post/1397409/


All Articles