0

这是我在UINavigationController子类中的代码的一部分。我创建了一个UIButton大多数时间都会显示的自定义。如何将其隐藏在特定视图中?我希望能够setHidden在一些 ViewControllers 中使用按钮。是UIButton一个属性。

-(void)viewDidLoad
{
    [super viewDidLoad];
    _coolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_coolBtn setFrame:CGRectMake(0, 0, 56, 39)];
    [_coolBtn setImage:[UIImage imageNamed:@"top.png"] forState:UIControlStateNormal];
    [_coolBtn addTarget:self action:@selector(doSomethingCool) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationBar addSubview:_coolBtn];
}

在我想隐藏按钮ViewDidLoad的地方添加这个:ViewController

SubClassUInav *test =[[SubClassUInav alloc]init];
[test.coolBtn setHidden:YES];

不工作。

编辑: 也许是因为我正在创建它的一个新实例?我没有在我的代码中引用这个子类。我所做的唯一一件事就是在选择 IB 时将其作为自定义类添加到 IBUINavigationController中。

4

3 回答 3

1

这是你必须做的。

SubClassUINav.h 中

@interface SubClassUInav : UINaviagationController {}

@property (nonatomic, strong) UIButton *coolBtn;

SubClassUINav.m 中

@synthesize _coolBtn = coolBtn;

在您的MyViewController.m中:

#import "SubClassUINav.h"    

// get reference of your nav controller, do not create new instance by alloc-init
    SubClassUINav *subClassUINavInstance = (SubClassUINav *) self.navigationController
    [subClassUINavInstance.coolBtn setHidden: YES]; //Access your properties

希望现在你能清楚地看到。

于 2013-06-11T09:47:36.153 回答
1

您也可以使用 NotificationCenter 来做到这一点,如下所示

从按钮定义类在 NSNotificationCenter 添加观察者:-

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(HideButton:)
                                                 name:@"HideButton"
                                               object:nil];

-(void)HideButton:(NSNotification *)notification {

    hide button code

}

使用波纹管代码调用它:-

[[NSNotificationCenter defaultCenter] postNotificationName:@"HideButton" object:self];
于 2013-06-11T09:52:00.463 回答
0

你做错的是这条线

SubClassUInav *test =[[SubClassUInav alloc]init];

这将创建一个新实例,在该实例中按钮状态将被隐藏。在您的类中,您将执行相同的操作,添加为子视图。使用该实例并将其隐藏

于 2013-06-11T09:48:09.987 回答