2015-12-21 54 views
1

我創建了MyCustomView.xib/h/m:它擴展了UIView類。然後在我的Main.storyboard中,放入UIView對象,將類更改爲MyCustomView並鏈接到MainController.h。所以,MainController包含對MyCustomView實例的引用。 對於從廈門國際銀行加載,在MyCustomView我做到以下幾點:委託正在發佈的情況下自定義查看使用XIB創建

- (instancetype)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
    if (self.subviews.count == 0) { 
     [self commonInit]; 
    } 
} 
return self; 
} 

- (instancetype)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    [self commonInit]; 
    } 
    return self; 
} 

- (void) stretchToSuperView:(UIView*)view 
{ 
    view.translatesAutoresizingMaskIntoConstraints = NO; 
    NSDictionary *bindings = NSDictionaryOfVariableBindings(view); 
    NSString *formatTemplate = @"%@:|[view]|"; 
    for (NSString * axis in @[@"H",@"V"]) { 
    NSString * format = [NSString stringWithFormat:formatTemplate,axis]; 
    NSArray * constraints = [NSLayoutConstraint  constraintsWithVisualFormat:format options:0 metrics:nil views:bindings]; 
    [view.superview addConstraints:constraints]; 
}  
} 

- (void)commonInit 
    { 
    MyCustomView* view = nil; 
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; 
    view = views.firstObject; 
    [self addSubview:view]; 
    [self stretchToSuperView:views.firstObject]; 
} 

這工作得很好,直到我想,以通知MainController任何變化(點擊按鈕等)申報委託在MyCustomView。所以,我的ManController符合MyCustomViewDelegate並實現方法。

編輯1個設置委託

//MainViewController.m file 
@interface MainViewController() 

@property (strong, nonatomic) IBOutlet MyCustomView *customView; 
@end 

@implementation MainViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.customView.delegate = self; 
} 

這裏的問題是,委託變成零,我不明白其中的道理,所以不知道什麼是錯誤的。

編輯2我覺得我有2個不同的MyCustomView實例。 我在MyCustomView增加新的屬性:

@interface MyCustomView : UIView 
@property (weak, nonatomic) IBOutlet UIButton *firstItem; 
@property(nonatomic, weak)id <MyCustomViewDelegate> delegate; 
// this is test property 
@property(nonatomic, assign)int testProperty; 

@end 

當我在viewDidLoad中設置該屬性,然後點擊第一個按鈕,我看到testProperty具有值0,所以,這可能意味着一些錯誤IBOutlet中MyCustomView * customView。

+0

@Rob是的,代表很弱。 ViewController不能被釋放,因爲它是前端控制器,它實際上是我的實驗項目中的單個控制器。 – someUser

+0

customView不是零。我將Main.storyboard的MyCustomView鏈接到控制器。 – someUser

回答

0

你是對的,你有兩個視圖對象。您添加到故事板的那個以及您通過loadNibNamed創建的那個。

底線,loadNibNamed將爲您創建視圖(假設你指定MyCustomView作爲在NIB指定的視圖的基類,你可以離開NIB的「文件所有者」空白)。然後,您可以編寫一個方便的方法在MyCustomView實例基於NIB視圖:

+ (instancetype)myCustomViewWithDelegate:(id<MyCustomViewDelegate>)delegate { 
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:delegate options:nil]; 
    MyCustomView *view = array.firstObject; 
    NSAssert([view isKindOfClass:[MyCustomView class]], @"Base class of NIB's top level view is not MyCustomView"); 
    view.delegate = delegate; 

    return view; 
} 

然後,而不是指定的故事板視圖,你必須實例並將其添加到視圖層次編程,例如:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    MyCustomView *view = [MyCustomView myCustomViewWithDelegate:self]; // since `self.myCustomView` should be `weak`, let's hold the view in local variable 
    [self.view addSubview:view]; 
    self.myCustomView = view;           // then set the property after the view is safely added to view hierarchy 

    NSDictionary *views = @{@"myCustomView" : self.myCustomView}; 
    self.myCustomView.translatesAutoresizingMaskIntoConstraints = false; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myCustomView]|" options:0 metrics:nil views:views]]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myCustomView]|" options:0 metrics:nil views:views]]; 

    // ... 
} 

顯然,編程添加視圖時,必須如上所示,以指定其frameautoresizingMask,或使用的限制。

0

不幸的是,我不能做一個簡單的評論,所以我不得不問,並給出建議作爲'答案'。

首先,我沒有看到你在哪裏設置委託給你的MainController,但我認爲在控制器的viewDidLoad()中。

第二件事。如何設置MyCustomView.xib非常重要,因爲您創建了一個全新的對象MyCustomView,並且其屬性將不可用於控制器。

MyCustomView* view = nil; NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; view = views.firstObject; [self addSubview:view];

不知道的廈門國際銀行的設置,並設置委託的方法,這裏是我的猜想有什麼可以幫助你:

  1. 在MyCustomView.xib類的rootView的設置UIView的。
  2. 在MyCustomView.xib中,將文件所有者的類設置爲MyCustomView
  3. 將您的子視圖創建爲UIView。

    UIView* view = nil; NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; view = views.firstObject; [self addSubview:view];

  4. 設置myCustomView對象在控制器的委託。

self.myCustomView.delegate = self;

我希望這會幫助,如果沒有,那麼請延長與委託二傳手代碼塊你的問題,你的廈門國際銀行設置方法。 (文件所有者的類,根視圖的類等)

相關問題