2014-12-05 112 views
5

我創建了UIView的自定義子類以及xib文件,並在自定義類中聲明瞭IBOutlets和IBActions。從Xib加載的自定義UIView

@interface ContactUsView : UIView 

@property (nonatomic, weak) IBOutlet UIButton *displayCloseButton; 

- (IBAction)callButtonPressed:(id)sender; 
- (IBAction)emailButtonPressed:(id)sender; 
- (IBAction)displayCloseButtonPressed:(id)sender; 

@end 

在xib文件中,我拖入一個UIView來表示我的自定義視圖。我已設置:

  • 文件所有者=我的自定義類
  • 已經設置了拖在UIView的到我的自定義類。

然後我添加了各種按鈕,這些按鈕連接到上述3種方法。

的ContactUsView.m裏面我有以下幾點:

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) { 
     NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil]; 
     for (id object in array) { 
      if ([object isKindOfClass:[ContactUsView class]]) 
       self = (ContactUsView *)object; 
     } 
    } 
    return self; 
} 

當我來到創建這個觀點我做到以下幾點:

- (void)viewWillAppear:(BOOL)animated 
{ 
    ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero]; 

    CGPoint origin = self.view.frame.origin; 
    CGSize size = self.view.frame.size; 
    [contactUs setFrame:CGRectMake(origin.x, 
           CGRectGetMaxY(self.view.frame) - 100, 
           size.width, 
           contactUs.frame.size.height)]; 

    [self.view addSubview:contactUs]; 
} 

問題 當我按下之一應用程序崩潰的按鈕: 線程1:EXC_BAD_ACCESS(code = 2,address = 0xb0c

任何人都可以幫助我。我覺得我可能在創建和加載xibs的自定義uiviews方面犯了一個錯誤。

如果您需要了解更多信息,請告訴我。非常感謝。

未來的參考 使用xib創建自定義視圖時不要設置文件所有者。相反,按照通常的方式創建所有的IBOutlet和IBActions,然後掛鉤它們打開Utilities選項卡並從那裏控制拖動。

enter image description here

+0

你能張貼整個錯誤?或者錯誤實際發生的路線?在XCode中啓用例外的斷點。 – KerrM 2014-12-05 11:24:29

+0

如果我在被調用的某個方法中放置了一個斷點,它從來沒有達到斷點。它只是與上面的代碼崩潰,沒有顯示在輸出中。我添加了「所有異常」斷點,並且仍然沒有顯示在輸出中。 – pls 2014-12-05 11:39:30

回答

4

•文件所有者=我的自定義類

錯誤。文件所有者應該是空的。視圖本身是文件的所有者。這意味着你應該在xib中連接ContactUsView的所有行動和網點。

[[一個NSBundle mainBundle] loadNibNamed:@ 「ContactUsView」 所有者:自選項:無]

...

自我=(ContactUsView *)對象;

在通過self作爲owner參數後。你改變它。這意味着之前分配的ContactUsViewself)將被銷燬,因爲-loadNibNamed:owner:options:不保留它。如果你申請我的第一個建議,你應該送nilowner參數

for這裏循環是沒有必要使用只是array[0],因爲這始終是你的看法,如果您有有效視圖層次在廈門國際銀行

+0

非常感謝Silmaril。這已經整理了我的問題。爲了在創建自定義視圖時的將來參考,您需要打開實用程序標記並從那裏控制拖動。 – pls 2014-12-05 13:35:16

0

您可以使用委託這 這是你如何能做到這

 @protocol CustomViewDelegate <NSObject> 
- (void)callButtonPressed:(id)sender; 
- (void)emailButtonPressed:(id)sender; 
- (void)displayCloseButtonPressed:(id)sender; 

@end 

@interface ContactUsView : UIView 

@property (nonatomic, weak) IBOutlet UIButton *displayCloseButton; 

@property (nonatomic, weak) id<CustomViewDelegate> ButtonDelegate; 

- (IBAction)callButtonPressed:(id)sender; 

- (IBAction)emailButtonPressed:(id)sender; 
- (IBAction)displayCloseButtonPressed:(id)sender; 


@end 

和英寸M檔

- (IBAction)callButtonPressed:(id)sender 
{ 
[self.ButtonDelegate callButtonPressed:sender]; 
} 

- (IBAction)emailButtonPressed:(id)sender{ 
[self.ButtonDelegate emailButtonPressed:sender]; 
} 

- (IBAction)displayCloseButtonPressed:(id)sender{ 
[self.ButtonDelegate displayCloseButtonPressed:sender]; 
} 

之後,僅僅設置與視圖 - 控制refrence委託,在這裏使用這些委託

- (void)viewWillAppear:(BOOL)animated 

{

ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero]; 
contactUs.ButtonDelegate = self; 

CGPoint origin = self.view.frame.origin; 
CGSize size = self.view.frame.size; 
[contactUs setFrame:CGRectMake(origin.x, 
          CGRectGetMaxY(self.view.frame) - 100, 
          size.width, 
          contactUs.frame.size.height)]; 

[self.view addSubview:contactUs]; 

}

- (void)callButtonPressed:(id)sender 

{}

- (void)emailButtonPressed:(id)sender 

{}

- (void)displayCloseButtonPressed:(id)sender 

{}

我已經做到了這一點和工作totlly精細

+0

感謝您的回覆。你是否像我一樣加載xib?當我點擊一個綁定到IBAction的按鈕時,我的應用程序崩潰。然而,它立即崩潰意味着方法中沒有任何事情發生。如果在我看來,像加載視圖時的內存一樣。 – pls 2014-12-05 12:57:19

+0

是的,我也這麼做。 – 2014-12-05 13:06:56

2

如果您正在加載一個XIB一個UIView,那麼你應該創建一個類的方法加載視圖。

在你customview.h

+(id)customView; 

&在customview.m

+ (id)customView 
{ 
    CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil] lastObject]; 
    if ([customView isKindOfClass:[CustomView class]]) 
     return customView; 
    else 
     return nil; 
} 

您可以在任何地方使用初始化:

CustomView *myView = [CustomView customView]; 

編輯:確保你已經改變了你的customview類的身份檢查&也確保您的IBActions的連接與該類的方法。 enter image description here

enter image description here

+0

嗨Ajay。是的,我可能會實現這個作爲一個類的方法。但是,您的方法中的代碼與我的代碼相同,並且仍導致相同的崩潰。 – pls 2014-12-05 13:25:47

+0

查看我的更新回答 – Ajay 2014-12-05 13:36:50

+0

Silmaril回答讓我意識到我做錯了什麼。在看到您編輯的答案之前,我已經接受了他的答案,它有相同的解決方案我也贊成你的答案。非常感謝。 – pls 2014-12-05 13:44:40