如何将值传递给子类中父类中定义的变量?

时间:2012-01-31 05:49:07

标签: iphone uibutton

我的主页有一个工具栏,其中包含三个按钮,通知和登录。我只想在用户进入时显示通知按钮。这是我的代码

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

}

//儿童班

@interface LoginScreen:工具栏{

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
}

}

问题是我能够回到家但是signIn条件不起作用。请指导我。感谢..

1 个答案:

答案 0 :(得分:1)

使用Protocol s(委托机制)将值发送回父视图控制器。

有关示例,请参阅here。另外here

您可以在每个Apple示例应用程序中找到许多样本。

我也给了你Sample Application。仔细检查并根据需要修改协议