无法隐藏以编程方式创建的UIButton?

时间:2013-04-29 16:42:34

标签: ios objective-c uibutton programmatically-created

LoginController尝试进行简单的登录尝试 - 如果成功,则会在setEmail中调用leftcontroller

调用setEmail时我需要做的就是隐藏btnLogin并显示buttonUser并且我尝试的任何内容都无效。 NSLog被调用且EventsController加载,因此setEmail正在运行 - 它不会隐藏并显示按钮。

leftcontroller.h

@interface LeftController : UIViewController <UIApplicationDelegate>
{
    IBOutlet UIButton *btnLogin;
    IBOutlet UIButton *buttonUser;
    NSString *setEmail;
}
@property (nonatomic,strong) IBOutlet UIButton *btnLogin;
@property (nonatomic,strong) IBOutlet UIButton *buttonUser;

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic) NSString *Email;

//-(void) setEmail : (NSString *) Email;

@end

leftcontroller.m

- (void) setEmail : (NSString * ) Email{
    [buttonUser setTitle:Email forState:UIControlStateNormal];
    //btnLogin.hidden = YES;
    //buttonUser.hidden = NO;
    [btnLogin setHidden:YES];
    [buttonUser setHidden:NO];

    NSLog(@"Set Email Called");
    NSLog(@"email: %@", Email);

    DDMenuController *menuController = (DDMenuController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).menuController;
    EventsViewController *controller = [[EventsViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

        [menuController setRootController:navController animated:YES];
} 

-(void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = HEXCOLOR(0x000000);

    btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(0, 0, 180.f, 48);
    [btnLogin setTitle:@"Login" forState:UIControlStateNormal];
    [btnLogin addTarget:self action:@selector(LoginPressed)
       forControlEvents:UIControlEventTouchUpInside];
    btnLogin.tag=1;

    btnLogin.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btnLogin setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    btnLogin.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    UIImage *btnImage = [UIImage imageNamed:@"nav_top.png"];
    [btnLogin setBackgroundImage:btnImage forState:UIControlStateNormal];
    [self.view addSubview:btnLogin];

    buttonUser = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonUser.frame = CGRectMake(0, 0, 180.f, 48);
    //[buttonUser setTitle:_Email forState:UIControlStateNormal];
    [buttonUser addTarget:self action:@selector(EmailPressed)
         forControlEvents:UIControlEventTouchUpInside];
    buttonUser.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [buttonUser setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    buttonUser.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    UIImage *btnImage1 = [UIImage imageNamed:@"nav_top.png"];
    [buttonUser setBackgroundImage:btnImage1 forState:UIControlStateNormal];
    [self.view addSubview:buttonUser];
    buttonUser.hidden = YES;
}

3 个答案:

答案 0 :(得分:4)

这条线是可疑的:

btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// ...
[self.view addSubview:btnLogin];

如果您要创建btnLogin并将其自己添加到self.view代码中,那么为什么它是一个出口?

@property (nonatomic,strong) IBOutlet UIButton *btnLogin;

插座适用于已经存在的内容,并在 nib 中实例化。因此,在我看来,你可能有两个btnLogin个对象,其中一个消除了你对另一个的引用。对于其他按钮也一样。

答案 1 :(得分:0)

您没有设置属性,请替换:

btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];

使用:

self.btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];

并定义您的属性,如:

@property (nonatomic,strong) UIButton *btnLogin;

buttonUser执行相同操作。

答案 2 :(得分:0)

设置按钮的hidden后,您会立即说出

[menuController setRootController:navController animated:YES];} 

这表明您正在更改界面。但是,如果您要更改界面,是否可以确定您的按钮是否已更改?我不知道你在这里做什么,但也许你的按钮不再在界面中了。也许您在界面中看到的是一组不同的按钮。