ios - 隐藏另一个类的自定义按钮

标签 ios objective-c cocoa-touch uinavigationcontroller

这是我在 UINavigationController 子类中的代码的一部分。 我创建了一个将在大部分时间显示的自定义 UIButton。 如何在特定 View 中隐藏它? 我希望能够 setHidden 某些 ViewController 中的按钮。 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];
}

在我想隐藏按钮的 ViewControllerViewDidLoad 中添加它:

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

不起作用。

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

最佳答案

这是你必须做的。

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

希望现在你能看清楚了。

关于ios - 隐藏另一个类的自定义按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040665/

相关文章:

ios - 为什么在针对模拟器时框架包路径解析为 DerivedData 构建目录?

ios - 在 iOS 上关闭流式视频(m3u8 文件)的隐藏式字幕按钮

ios - 如何使用 PHAssetResourceRequestOptions progressHandler/PHAssetResourceProgressHandler

objective-c - 退出应用程序的方法之间的区别 : exit(), NSApp/NSApplication terminate

ios - UITableView :reloadSectionIndexTitles animated correct way

ios - 重新加载 Collection View 数据时对成员 'collectionView(_:numberOfItemsInSection:)' 的引用不明确

javascript - JqueryMobile加载图标不一致

objective-c - 将 Storyboard 更新到 iOS 6 并向后兼容

ios - 如何在 UISearchBar 中从自定义 inputView 动态更改为默认 inputView?

iphone - 将iphone摄像头集成到ViewController的 subview UIView中