从 XIB 加载 UIView
Load UIView from XIB
我正在寻找有关如何从 XIB 文件加载 UIView 的建议。我尝试使用此 tutorial.
中的第 5 个解决方案
但是,按钮操作将不起作用。我几乎完全按照教程设置我的项目,但 IBAction 根本不会被调用。我不确定自适应布局是否导致了这种怪异。
您能否快速浏览一下我的项目中发生了什么?我创建了一个小项目只是为了让按钮正常工作 placed here.
这是代码片段:
@interface ProfileHeadingViewOwner : NSObject
@property (nonatomic, weak) IBOutlet ProfileHeadingView *profileHeadingView;
@end
@implementation ProfileHeadingViewOwner
@end
static ProfileHeadingViewOwner* __owner = nil;
@interface ProfileHeadingView ()
@property (nonatomic, weak) IBOutlet UILabel *profileName;
@property (nonatomic, weak) UIViewController <ProfileHeadingViewDelegate> *delegateViewController;
@end
@implementation ProfileHeadingView
+(void)presentInViewController:(UIViewController<ProfileHeadingViewDelegate>*) viewController
{
// Instantiating encapsulated here.
//ProfileHeadingViewOwner *owner = [ProfileHeadingViewOwner new];
__owner = [ProfileHeadingViewOwner new];
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:__owner options:nil];
// Pass in a reference of the viewController.
__owner.profileHeadingView.delegateViewController = viewController;
// Add (thus retain).
[viewController.view addSubview:__owner.profileHeadingView];
}
+(void)setProfileName:(NSString*)name {
__owner.profileHeadingView.profileName.text = name;
}
-(IBAction)editAvatarButtonPressed:(UIButton *)sender {
////// THIS METHOD WON'T GET CALLED. WHY? ///////
[self.delegateViewController uploadProfileAvatar:self];
}
editAvatarButtonPressed
: 单击按钮时不会调用方法。
提前致谢。
禄范
你的代码中有很多问题。
- 您没有为
__owner.profileHeadingView
分配任何值
- 你的xib不对,你顶了
UIView
。你需要删除它。
要解决第一个问题,您需要执行以下操作:
__owner = [ProfileHeadingViewOwner new];
__owner.profileHeadingView = (ProfileHeadingView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:__owner options:nil] objectAtIndex:0];
__owner.profileHeadingView.delegateViewController = viewController;
要解决第二个问题,请更改您的 xib 设计:
至:
我正在寻找有关如何从 XIB 文件加载 UIView 的建议。我尝试使用此 tutorial.
中的第 5 个解决方案但是,按钮操作将不起作用。我几乎完全按照教程设置我的项目,但 IBAction 根本不会被调用。我不确定自适应布局是否导致了这种怪异。
您能否快速浏览一下我的项目中发生了什么?我创建了一个小项目只是为了让按钮正常工作 placed here.
这是代码片段:
@interface ProfileHeadingViewOwner : NSObject
@property (nonatomic, weak) IBOutlet ProfileHeadingView *profileHeadingView;
@end
@implementation ProfileHeadingViewOwner
@end
static ProfileHeadingViewOwner* __owner = nil;
@interface ProfileHeadingView ()
@property (nonatomic, weak) IBOutlet UILabel *profileName;
@property (nonatomic, weak) UIViewController <ProfileHeadingViewDelegate> *delegateViewController;
@end
@implementation ProfileHeadingView
+(void)presentInViewController:(UIViewController<ProfileHeadingViewDelegate>*) viewController
{
// Instantiating encapsulated here.
//ProfileHeadingViewOwner *owner = [ProfileHeadingViewOwner new];
__owner = [ProfileHeadingViewOwner new];
[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:__owner options:nil];
// Pass in a reference of the viewController.
__owner.profileHeadingView.delegateViewController = viewController;
// Add (thus retain).
[viewController.view addSubview:__owner.profileHeadingView];
}
+(void)setProfileName:(NSString*)name {
__owner.profileHeadingView.profileName.text = name;
}
-(IBAction)editAvatarButtonPressed:(UIButton *)sender {
////// THIS METHOD WON'T GET CALLED. WHY? ///////
[self.delegateViewController uploadProfileAvatar:self];
}
editAvatarButtonPressed
: 单击按钮时不会调用方法。
提前致谢。 禄范
你的代码中有很多问题。
- 您没有为
__owner.profileHeadingView
分配任何值
- 你的xib不对,你顶了
UIView
。你需要删除它。
要解决第一个问题,您需要执行以下操作:
__owner = [ProfileHeadingViewOwner new];
__owner.profileHeadingView = (ProfileHeadingView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:__owner options:nil] objectAtIndex:0];
__owner.profileHeadingView.delegateViewController = viewController;
要解决第二个问题,请更改您的 xib 设计:
至: