2013-08-16 159 views
0

我正在創建一個應用程序,其中幾乎所有的視圖都具有相同的背景和同一組按鈕,這些按鈕提交相同的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

+1

澄清你的問題。你想要在哪裏導航控制器?以及你如何使用這個子類uiview。 –

+0

更新了問題 – Sekhar

+0

您需要確保您的UI元素是您的自定義視圖的屬性 – Woodstock

回答

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

#進口 接口BMBackGroundView:UIView的{ } @屬性(非原子,保留)的UIImageView * ImageView的; @property(nonatomic,retain)UIButton * closeButton; @property(nonatomic,retain)UIButton * menuButton; @end – Sekhar

+0

替換這個UIView * learnMoreView = [BMBackGroundView new];用BMBackGroundView * –

+0

* learnMoreView = [BMBackGroundView new]; –

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]; 
    } 
+0

您似乎在回答框中發佈了一個問題。請編輯回答問題,或者刪除它。 –

+0

我想訪問uiviewcontroller的自定義視圖uielements – Sekhar

+0

@ sekhar- - 我已經更新了我的答案。試試看。 –

相關問題