0

我正在创建一个应用程序,其中几乎所有视图都具有相同的背景和相同的一组按钮,它们提交相同的操作。所以我正在创建一个具有相同背景和按钮的自定义视图。

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];

}

注意:我没有使用界面生成器

4

2 回答 2

1

问题出在这行代码中

//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... 


}

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

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

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

于 2013-08-16T16:35:15.923 回答
0

在 xib 中添加一个 uiview 元素并将其类更改为自定义视图类名称。然后您将能够从自定义视图类访问实例。

或者不是创建一个 uiview 类型的对象,而是创建一个类似的对象

Customview * newView = [customview new];

我试过了,我可以访问它。你做错了什么。确保您为关闭和菜单按钮正确编写了访问器方法。

-(void)loadView

{ 

     BKCCustomView *learnMoreView = [BKCCustomView new]; 

     self.view = learnMoreView; 

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

      [learnMoreView.menuButton setBackgroundColor:[UIColor lightGrayColor]]; //i am able to access it 
    [learnMoreView release]; 
    }
于 2013-08-16T14:35:32.497 回答