我正在创建一个应用程序,其中几乎所有视图都具有相同的背景和相同的一组按钮,它们提交相同的操作。所以我正在创建一个具有相同背景和按钮的自定义视图。
customView.h
@interface BMBackGroundView : UIView{
}
@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, retain) UIButton *closeButton;
@property(nonatomic, retain) UIButton *menuButton;
自定义视图.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(104, 36, 275, 33)];
[self addSubview:self.imageView];
CGRect frameForCloseButton = CGRectMake(44, 0, 48, 44);
CGRect frameForMenuButton = CGRectMake(382, 0, 48, 44);
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton setFrame:frameForCloseButton];
[self.closeButton setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
[self.closeButton addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.closeButton];
self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.menuButton setFrame:frameForMenuButton];
[self.menuButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
[self.menuButton addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.menuButton];
}
return self;
}
-(void)close{
NSLog(@"close button pressed");
}
-(void)showMenu:(id)sender{
NSLog(@"menu button pressed");
}
我能够在 viewController 中获取自定义视图,但我想以我无法执行的两种方法访问导航控制器,并且我无法访问 viewControllers 中自定义视图的 UI 元素。
在关闭和 showMenu Method.I want
[self.navigationController popToRootViewControllerAnimated:YES];
在我得到这个自定义视图的 ViewController.m 中,我想隐藏其中一个按钮。
-(void)loadView{
UIView *learnMoreView = [BMBackGroundView new];
self.view = learnMoreView;
learnMoreView.uielementOfCustomView //which i am not able to access
[learnMoreView release];
}
注意:我没有使用界面生成器