访问自定义视图

时间:2013-08-16 14:30:38

标签: ios objective-c uiview uiviewcontroller

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

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元素。 / p>

在close和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];

}

注意:我没有使用Interface Builder

2 个答案:

答案 0 :(得分: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文件....很容易分辨出来的是什么......

答案 1 :(得分: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]; 
    }