使用委托与“财产反向范围”

时间:2014-01-20 04:55:22

标签: objective-c inheritance ios7 delegates delegation

我有从VC1到VC2的推送segue。在准备segue转到VC2时,我使用“=”运算符设置VC2的属性,其中包含VC1中属性的内容。然后我在VC2中更改了该属性的内容。我现在需要将该属性传递回VC1。我的印象是我可以通过代表团来做到这一点。然而,一旦我在VC2中更改它,它似乎“神奇地”改变它自己。我真的不知道这个术语是什么。它必须是“=”运算符为VC1和VC2中的属性分配相同的内存位置,使两者都指向它?请让我知道这个术语,以及我是应该保留这个还是重做我的模型,所以我使用委托。

感谢。

EDIT /增加:

因此VC1 prepareForSegue中的这个属性1设置了VC2的property1。然后我在VC2的textFieldDidEndEditing中更改property2。当我运行程序时,property1在VC1中被静态分配。然后在更改之前检查VC2中的property1,它没关系,然后我在修改后检查它并且它被正确修改。但是,当我回到VC1(通过导航控制器中的后退按钮)时,我检查VC1的property1,它是新的修改版本。我以为我必须使用委托将它传递给VC1。

我的问题是,这叫什么?某种范围或继承?我应该以这种方式设置我的程序,还是应该强制它使用委托从视图到模型?

VC1.M

@interface VC1()
@property(nonatomic, strong) NSMutableDictionary *property1;
@end

@implementation VC1

@synthesize property1= _property1;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"VC2_SEGUE"]){
        VC2 *mVC2 = (VC2 *)segue.destinationViewController;
        mVC2.property1 = self.property1;
    }
}

@end

VC2.H

@interface VC2: UITableViewController <UITextFieldDelegate>
@property(nonatomic, strong)NSMutableDictionary *property1;
@end

VC2.M

@implementation VC2

@synthesize property1 = _property1;

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    // Get the cell in which the textfield is embedded
    id textFieldSuper = textField;
    while (![textFieldSuper isKindOfClass:[CustomCel class]]) {
        textFieldSuper = [textFieldSuper superview];
    }
    // Get that cell's index path

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell   *)textFieldSuper];

    NSMutableArray *newObject = [[self.property1 objectForKey:[NSString stringWithFormat:@"%i", indexPath.section]] mutableCopy];
    [newObject replaceObjectAtIndex:indexPath.row withObject:textField.text];

    [self.property1 setObject:newObject forKey:[NSString stringWithFormat:@"%i", indexPath.section]];

}

@end

编辑:

我真的更好奇为什么在VC1中修改后,为什么在VC1中更改了property1。我只使用NSString做了同样的事情,并将VC2中的属性分配给本地val,但是一旦我回到VC1就没有坚持。请解释一下:(

1 个答案:

答案 0 :(得分:0)

Delegate是一种在VC2代码区域中设置VC1属性的样式。 使用块可以使您的代码更清晰。 在VC2.h文件中

typedef void(^BeginEditBlock )(NSMutableArray *newObject, int section);

@interface VC2: UITableViewController <UITextFieldDelegate>
@property(nonatomic, strong) BeginEditBlock *editBlock;
@end
VC2.m文件中的

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    // Get the cell in which the textfield is embedded
    id textFieldSuper = textField;
    while (![textFieldSuper isKindOfClass:[CustomCel class]]) {
        textFieldSuper = [textFieldSuper superview];
    }
    // Get that cell's index path

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell   *)textFieldSuper];

    NSMutableArray *newObject = [[self.property1 objectForKey:[NSString stringWithFormat:@"%i", indexPath.section]] mutableCopy];
    [newObject replaceObjectAtIndex:indexPath.row withObject:textField.text];

    if(self.editBlock){
        self.editBlock(newObject, indexPath.section);
    }
}

在VC1.m文件中。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"VC2_SEGUE"]){
        VC2 *mVC2 = (VC2 *)segue.destinationViewController;
        mVC2.editBlock = ^(NSMutableArray *newObject, int section){
            [self.property1 setObject:newObject forKey:[NSString stringWithFormat:@"%i", section]];

       };
    }
}

关于阻止说明是:here

相关问题