我正在創建一個應用程序,其中幾乎所有的視圖都具有相同的背景和同一組按鈕,這些按鈕提交相同的action.so我使用相同的背景和按鈕創建自定義視圖。訪問自定義視圖
customView.h
@interface BMBackGroundView : UIView{
}
@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, retain) UIButton *closeButton;
@property(nonatomic, retain) UIButton *menuButton;
customView.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中訪問自定義View的UI元素。
密切和showMenu Method.I想
[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];
}
注:我沒有使用Interface Builder
澄清你的問題。你想要在哪裏導航控制器?以及你如何使用這個子類uiview。 –
更新了問題 – Sekhar
您需要確保您的UI元素是您的自定義視圖的屬性 – Woodstock