Can't change value of BOOL from another class, IOS

旧城冷巷雨未停 提交于 2020-01-16 00:56:08

问题


I encounter a simple problem , I am trying to change the bool value of a class from another class but it is not happening, it returns NO.

//before pushing other 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];
    }


}

Whats wrong with the code?

+++++++++ANSWER ADDED+++++++++++++++++

thanks to Joel here is the answer

-(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;
    }
}

回答1:


I think the issue is that you alloc and init AddGirlsView. When you perform a segue it is probably re-instantiating another AddGirlsView. You should be setting this variable in prepareForSegue.

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



回答2:


@property (nonatomic) BOOL isEditModeON;


来源:https://stackoverflow.com/questions/14568469/cant-change-value-of-bool-from-another-class-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!