简体   繁体   English

iOS更改父ViewController的Integer / NSNumber

[英]iOS change Integer/NSNumber of parent ViewController

I have a viewController with a property of NSInteger (or NSNumber), What I want to do is pass the integer to the child ViewController, do some change (maybe add by some number). 我有一个具有NSInteger(或NSNumber)属性的viewController,我想要做的是将整数传递给子ViewController,做一些更改(可能添加一些数字)。

But when I use NSNumber as property in the parent and child controller and pass it by set property: 但是当我在父控制器和子控制器中使用NSNumber作为属性并通过set属性传递它时:

//parent and child property.    
@property (nonatomic,strong) NSNumber *num;

//pass to the child
child.num = self.num;

//do some change in child
self.num = [NSNumber numberWithInteger:[self.num integerValue] + 1];

This cannot change the value of the parent , because NSNumber is readonly and in the child they alloc a new one, without change the value of the parent. 这不能更改父级的值,因为NSNumber是只读的,并且在子级中它们分配了一个新的,而不更改父级的值。

So: 所以:

How I can pass a NSInteger or NSNumber to the child ViewController and change the value in it, and when child is pop away, the parent controller get the change? 我如何将NSInteger或NSNumber传递给子ViewController并更改其中的值,当子弹出时,父控制器得到更改?

You are defining and assigning a new NSNumber in the following line: 您正在以下行中定义和分配新的NSNumber:

self.num = [NSNumber numberWithInteger:[self.num integerValue] + 1];

You can't use this method because NSNumbers are immutable. 您不能使用此方法,因为NSNumbers是不可变的。 So self.num will point to another location different from parent's num . 所以self.num将指向与父的num不同的另一个位置。 To avoid problems like this you can use delegate pattern or simply passing parent's viewController pointer to the child and changing its num value. 为了避免这样的问题,你可以使用委托模式或简单地将父的viewController指针传递给子节点并更改其num值。

For example: 例如:

Parent's Header: 家长的标题:

@property (nonatomic,strong) NSNumber *num;

Child's Header: 孩子的标题:

@property (nonatomic,weak) parentViewControllerClass * parentClass;

Code: 码:

self.parentClass.num = [NSNumber numberWithInteger:[self.parentClass.num integerValue] + 1];

PS You can check out how to define protocols here and here . PS您可以在这里这里查看如何定义协议。 With that you can implement the delegate pattern. 有了它,您可以实现委托模式。

Maybe in other language you could pass pointer to pointer (I'm not sure if you should ), but in Objective-C you can't request address of property. 也许在其他语言中你可以将指针传递给指针(我不确定你是否应该 ),但在Objective-C中你不能请求属性的地址。

What you can do is: 可以做的是:

  1. Use mentioned delegate pattern - what it means: 使用提到的委托模式 - 它意味着什么:
    1. Create protocol that specifies method like setNumber: 创建指定setNumber:方法的协议setNumber:
    2. Implement this protocol in your parent controller (just implement setNumber: function) 在父控制器中实现此协议(只需实现setNumber: function)
    3. Set parent controller as property of child, but as instance of this protocol, not instance of it's class - to keep encapsulation. 将父控制器设置为子属性,但作为此协议的实例,而不是它的类的实例 - 以保持封装。
    4. Call setNumber: from child controller 调用setNumber:来自子控制器
  2. Create mutable object that is shared between parent and child controller. 创建父控制器和子控制器之间共享的可变对象。 For example: 例如:

     @interface Counter : NSObject @property (nonatomic) NSInteger value; @end 
  3. You could post notification about number change from child controller and observe it in parent: 您可以从子控制器发布有关号码更改的通知,并在父级中观察它:

     // post notification in child NSNumber *num = [NSNumber numberWithInteger:1]; [[NSNotificationCenter defaultCenter] postNotificationName:@"numberChangedNotification" object:self userInfo:@{@"number": num}]; // observe notifications in parent // first register observer in viewDidLoad [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNumber:) name:@"numberChangedNotification" object:child]; // and implement updateNumber - changeNumber: (NSNotification *)notification { NSNumber *num = notification.userInfo[@"number"]; // ... } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM