无法从另一个类IOS更改BOOL的值

时间:2013-01-28 18:17:10

标签: ios objective-c boolean

我遇到一个简单的问题,我试图从另一个类改变一个类的bool值,但它没有发生,它返回NO

//在推送其他viewcontroller之前

-(IBAction)editWOD
{
    self.editGirlController= [[AddGirlsView alloc] init];
    self.editGirlController.isEditModeON=YES;
    self.editGirlController.editgirls=girls;
    [self performSegueWithIdentifier:@"editModeGirls" sender:self];
}

AddGirlsView .h

@interface AddGirlsView : UIViewController
{
    BOOL isEditModeON;

}
@property BOOL isEditModeON;

AddGirlsView .m

@synthesize isEditModeON;
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (isEditModeON==NO) {
        NSLog(@"Edit Mode is OFF");
    }
    else{
        [self populateViewOnEdit];
    }


}

代码有什么问题?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

感谢Joel,这是答案

-(IBAction)editWOD
{
    [self performSegueWithIdentifier:@"editModeGirls" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"editModeGirls"])
    {
        AddGirlsView *editGirlController  = [segue destinationViewController];
        editGirlController.isEditModeON=YES;
        editGirlController.editgirls=girls;
    }
}

2 个答案:

答案 0 :(得分:4)

我认为问题是你分配并初始化AddGirlsView。当你执行segue时,它可能会重新实例化另一个AddGirlsView。您应该在prepareForSegue中设置此变量。

if ([segue.identifier isEqualToString:@"editGirlsMode"]) {
    [segue.destinationViewController setIsEditModeOn:YES];
    [segue.destinationViewController setEditGirls:girls];
}

答案 1 :(得分:-4)

@property (nonatomic) BOOL isEditModeON;