Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value to a variable defined in parent class from child class?

My home page has a toolbar with three buttons home,notification and login. I want notification button show only when user loged In. here is my code

//Parent class

@interface Toolbar : UIViewController {
UIToolbar *toolBar;
UIButton *home;
UIButton *notification;
    UIButton *login;
BOOL signIn;

}

@property(nonatomic,assign)BOOL signIn;
@property(nonatomic,retain)UIButton *home;
@property(nonatomic,retain)UIButton *notification;
@property(nonatomic,retain)UIButton *login;
@property(nonatomic,retain)UIToolbar *toolBar;


-(IBAction)Home:(id)sender;
-(IBAction)LogIn:(id)sender;

//Toolbar.m

 -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];


if (!signIn) {
    [notification setHidden:YES];
    [login setHidden:NO];
}else {
    [notification setHidden:NO];
    [login setHidden:YES];
}

}

//Child class

@interface LoginScreen : Toolbar{

LoginScreen.m

-(IBAction)SignIn{
if ([emailField.text isEqualToString:@"m"] && [passwordField.text isEqualToString:@"a"]) {
    NSLog(@"loggedIN");
    signIn=YES;

    Home *home=[[Home alloc]initWithNibName:@"Home" bundle:nil];

    [self.navigationController pushViewController:home animated:YES];
    [home release];
}else {
//other condition
}

}

problem is I am able to to go back to home but signIn condition is not working. please guide me . Thanks..

like image 945
NoviceDeveloper Avatar asked Jan 22 '26 10:01

NoviceDeveloper


1 Answers

Use Protocols (delegate mechanism) to send back the value to the parent view controller.

See here for some sample. Also here

You can find many samples for this in every Apple sample application.

I have also give you a Sample Application. Go through that and modify the protocol as per your need

like image 138
Ilanchezhian Avatar answered Jan 25 '26 10:01

Ilanchezhian