我絕望地卡住了,我將不勝感激一個指針。自定義UIView子類需要awakeFromNIB被調用兩次
我們試圖構建一個包含多個視圖的視圖控制器,這些視圖是UIView的一個子類。除了我們需要手動初始化視圖或再次手動調用awakeFromNib(這是不允許的)之外,所有的工作都「很好」。
這裏的問題是...
子類的UIView頭文件:
#import <UIKit/UIKit.h>
@interface introView : UIView
@property (strong, nonatomic) UIView *view;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@end
子類的UIView主文件:
#import "introView.h"
@interface introView()
@end
@implementation introView
-(id)init
{
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"introView" owner:self options:nil];
id mainView = [subviewArray objectAtIndex:0];
return mainView;
}
- (void) awakeFromNib
{
[super awakeFromNib];
[self initialize];
[self addSubview:self.view];
}
-(void)initialize
{
NSLog(@"title: %@", self.title);
self.titleLabel.text = self.title;
}
然後我們從一個視圖控制器這樣做初始化視圖:
introView *view = [[introView alloc]init];
view.title = @"Test";
[self addSubview:view];
這裏是問題 - 通過調用這個視圖,視圖正確顯示,但標題爲NULL;
[43605:1957090] title: (null)
然而,如果我們調用awakeFromNib再次,那麼該觀點正確
初始化在視圖控制器:
introView *view = [[introView alloc]init];
view.title = @"Test;
[self addSubview:view];
[view awakeFromNib];
然後,它的工作原理:
2014-11-21 09:33:03.500 Test[43706:1972060] title: (null)
2014-11-21 09:33:03.500 Test[43706:1972060] title: Test
或者,如果我做初始化方法public並在初始化之後調用它以及。但是,這違背了我眼中的目的...
在視圖控制器:
introView *view = [[introView alloc]init];
view.title = @"Test;
[self addSubview:view];
[view initialize]; //calling the method directly...
看來我,我們在某種程度上運行到哪裏的觀點還沒有準備好條件,但隨後在工作第二次嘗試(即使awakeFromNib調用是非法的)
再次,插座設置正確,文件所有者被設置...它只需要兩個awakeFromNib調用。
任何幫助表示讚賞。
---------------- UPDATE ------------------
謝謝你們,我欣賞指針。我按照概述實現了初始化程序,但是我遇到了同樣的問題。此外,我使用GitHub示例作爲一個乾淨的示例,並試圖爲此視圖分配一個變量,甚至顯示相同的行爲。因此我開始認爲我在其他地方做錯了事。看到這裏:
//
// SubClassView.h
// CustomView
//
// Created by Paul Solt on 4/28/14.
// Copyright (c) 2014 Paul Solt. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "PSCustomViewFromXib.h"
@interface SubClassView : PSCustomViewFromXib
@property (strong, nonatomic) NSString *title;
@end
SubclassView。米
//
// SubClassView.m
// CustomView
//
// Created by Paul Solt on 4/28/14.
// Copyright (c) 2014 Paul Solt. All rights reserved.
//
#import "SubClassView.h"
@interface SubClassView()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UISwitch *onSwitch;
@end
@implementation SubClassView
// Note: You can customize the behavior after calling the super method
// Called when loading programatically
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
// Call a common method to setup gesture and state of UIView
[self setup];
}
return self;
}
// Called when loading from embedded .xib UIView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
// Call a common method to setup gesture and state of UIView
[self setup];
}
return self;
}
- (void)setup {
// Add a gesture to show that touch input works on full bounds of UIView
NSLog(@"%@", self.title);
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
}
- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture {
NSLog(@"Pan: %@", NSStringFromCGPoint([gesture locationInView:gesture.view]));
}
- (IBAction)switchChanged:(UISwitch *)sender {
NSLog(@"Switch: %d", sender.on);
}
@end
視圖控制器類
//
// ViewController.m
// CustomView
//
// Created by Paul Solt on 4/28/14.
// Copyright (c) 2014 Paul Solt. All rights reserved.
//
#import "ViewController.h"
#import "SubClassView.h"
#import "LabelMadness.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Add a custom view's programmatically (position using 1/2 width and height)
SubClassView *oneView = [[SubClassView alloc] init];
oneView.center = CGPointMake(80 + 80, 282 + 40);
oneView.backgroundColor = [UIColor blueColor];
oneView.title = @"Test2";
[self.view addSubview:oneView];
}
@end
-------輸出-----
2014-11-21 11:49:38.893 CustomView[45653:2123668] LabelMadness.initWithFrame:
2014-11-21 11:49:38.893 CustomView[45653:2123668] (null)
我在做什麼錯在這裏?
你的代碼有點混亂。在introView類中,什麼是self.view(在[self addSubview:self.view]中)? mainView的外觀是什麼(它有哪些子視圖)? – rdelmar 2014-11-21 18:38:18