[英]Custom UIView loaded from Xib
我已經創建了一個UIView的自定義子類以及一個xib文件,並在自定義類中聲明了IBOutlets和IBActions。
@interface ContactUsView : UIView
@property (nonatomic, weak) IBOutlet UIButton *displayCloseButton;
- (IBAction)callButtonPressed:(id)sender;
- (IBAction)emailButtonPressed:(id)sender;
- (IBAction)displayCloseButtonPressed:(id)sender;
@end
在xib文件中,我拖入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(代碼= 2,地址= 0xb0c)
誰能幫我這個。 我覺得我可能在某處創建和加載來自xibs的自定義uiviews時犯了一個錯誤。
如果您需要更多信息,請告訴我們。 非常感謝。
未來參考使用xib創建自定義視圖時請勿設置文件所有者。 而是像往常一樣創建所有IBOutlets和IBActions,然后將它們連接起來打開Utilities選項卡並從那里控制拖動。
•Files owner =我的自定義類
錯誤。 文件所有者應為空。 視圖本身是文件所有者。 這意味着您應該在xib中使用ContactUsView
連接所有操作和出口。
[[NSBundle mainBundle] loadNibNamed:@“ContactUsView”owner:self options:nil]
...
self =(ContactUsView *)對象;
將self
作為owner
參數傳遞后。 你改變它。 這意味着以前分配的ContactUsView
( self
)將被破壞,因為-loadNibNamed:owner:options:
不保留它。 如果您應用我的第一個建議,您應該發送nil
作為owner
參數
這里的for
循環不一定只使用array[0]
,因為如果你的xib中有有效的視圖層次結構,這總是你的視圖
如果要為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與該類的方法的連接。
你可以使用委托這就是你如何做到這一點
@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];
}
之后,只需使用viewcontroller參考設置委托,並在此處使用這些委托
- (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
{}
我做到了這一點並且工作得非常好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.