gpt4 book ai didi

ios - 访问自定义 View

转载 作者:行者123 更新时间:2023-11-29 03:42:06 24 4
gpt4 key购买 nike

我正在创建一个应用程序,其中几乎所有 View 都具有相同的背景和相同的一组按钮,这些按钮执行相同的操作。因此我正在创建一个具有相同背景和按钮的自定义 View 。

customView.h

@interface BMBackGroundView : UIView{

}

@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, retain) UIButton *closeButton;
@property(nonatomic, retain) UIButton *menuButton;

自定义View.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 中获取自定义 View ,但我想在这两种方法中访问导航 Controller ,而我无法做到,而且我无法在 viewController 中访问自定义 View 的 UI 元素。

在close和showMenu方法中。我想要

    [self.navigationController popToRootViewControllerAnimated:YES];

在 ViewController.m 中,我获取此自定义 View ,我想隐藏其中一个按钮。

-(void)loadView{
UIView *learnMoreView = [BMBackGroundView new];
self.view = learnMoreView;

learnMoreView.uielementOfCustomView //which i am not able to access


[learnMoreView release];

}

注意:我没有使用 Interface Builder

最佳答案

问题出在这行代码

//this is completely wrong 
UIView *learnMoreView = [BMBackGroundView new];

您正在创建 UIView 的新实例。您应该创建 BMBackGroundView 的新实例。

你的代码应该是

BMBackGroundView *learnMoreView = [BMBackGroundView new];

这应该有效。并且还要确保在 customView .m

@synthesize closeButton, imageView, menuButton;

在访问其他类的元素时请记住......

这是您必须做的...您想要从其他类访问的任何元素都必须将它们公开

          @interface BMBackGroundView :UIView{
//Define only private variable here
UIButton * closeButton;
//If you have define closeButton here please delete it,
//don't define same variable as public and private...


}

//定义您想从其他类访问的所有公共(public)变量,如下所示

@property (nonatomic, strong) UIButton *close Button;

我希望这会有所帮助...如果没有,请分享您的 .h 文件...很容易知道出了什么问题...

关于ios - 访问自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com