从Xib加载的自定义UIView
我创建了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选项卡并从那里控制拖动。
•文件所有者=我的自定义类
错误。文件所有者应该是空的。视图本身是文件的所有者。这意味着你应该在xib中连接ContactUsView
的所有行动和网点。
[[一个NSBundle mainBundle] loadNibNamed:@ “ContactUsView” 所有者:自选项:无]
...
自我=(ContactUsView *)对象;
在通过self
作为owner
参数后。你改变它。这意味着之前分配的ContactUsView
(self
)将被销毁,因为-loadNibNamed:owner:options:
不保留它。如果你申请我的第一个建议,你应该送nil
为owner
参数
for
这里循环是没有必要使用只是array[0]
,因为这始终是你的看法,如果您有有效视图层次在厦门国际银行
非常感谢Silmaril。这已经整理了我的问题。为了在创建自定义视图时的将来参考,您需要打开实用程序标记并从那里控制拖动。 – pls 2014-12-05 13:35:16
您可以使用委托这 这是你如何能做到这
@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精细
感谢您的回复。你是否像我一样加载xib?当我点击一个绑定到IBAction的按钮时,我的应用程序崩溃。然而,它立即崩溃意味着方法中没有任何事情发生。如果在我看来,像加载视图时的内存一样。 – pls 2014-12-05 12:57:19
是的,我也这么做。 – 2014-12-05 13:06:56
如果您正在加载一个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的连接与该类的方法。
你能张贴整个错误?或者错误实际发生的路线?在XCode中启用例外的断点。 – KerrM 2014-12-05 11:24:29
如果我在被调用的某个方法中放置了一个断点,它从来没有达到断点。它只是与上面的代码崩溃,没有显示在输出中。我添加了“所有异常”断点,并且仍然没有显示在输出中。 – pls 2014-12-05 11:39:30