簡體   English   中英

從XIB加載UIView

[英]Load UIView from XIB

我正在尋找有關如何從XIB文件加載UIView的一些建議。 我嘗試了本教程中的第5個解決方案。

但是,按鈕操作將不起作用。 我幾乎完全按照教程設置了我的項目,但是IBAction根本不會被調用。 我不確定自適應布局是否會引起這種怪異。

您足夠友善地快速看一下我的項目中發生了什么嗎? 我創建了一個小項目來使按鈕正常工作,並將其放置在此處。

這是代碼片段:

@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 :方法。

提前致謝。 Loc Pham

您的代碼中有很多問題。

  1. 您沒有為__owner.profileHeadingView分配任何值
  2. 您的xib不正確,您有一個頂部UIView 您需要刪除它。

要解決第一個問題,您需要執行以下操作:

__owner = [ProfileHeadingViewOwner new];
__owner.profileHeadingView = (ProfileHeadingView *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:__owner options:nil] objectAtIndex:0];
__owner.profileHeadingView.delegateViewController = viewController;

要解決第二個問題,請從以下位置更改您的xib設計:

當前

至:

正確

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM