我绝望地被困住了,我会很感激指针。
我们正在尝试构建一个包含多个视图的视图控制器,这些视图是UIView的子类。一切正常,#34;以及#34;除了我们需要手动初始化视图或再次手动调用awakeFromNib这一事实,这是不允许的。
这是问题......
Subclassed 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
Subclassed 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
或者,如果我将initialize方法设为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.m
//
// 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
------- ----- OUTPUT
2014-11-21 11:49:38.893 CustomView[45653:2123668] LabelMadness.initWithFrame:
2014-11-21 11:49:38.893 CustomView[45653:2123668] (null)
我在这里做错了什么?
答案 0 :(得分:2)
您根本不需要使用awakeFromNib。如果你想让introView加载自己的nib,你可以这样做(将视图的类更改为xib文件中的introView),
-(instancetype)init{
if (self = [super init]) {
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"introView" owner:self options:nil];
self = [subviewArray objectAtIndex:0];
}
return self;
}
要设置标题,您可以覆盖标题属性的设置者
-(void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = title;
}
控制器中的代码与您最初尝试的代码相同。
答案 1 :(得分:0)
那么,你有.xib或故事板中的视图吗?
它看起来像是来自.xib所以你应该看看this answer
你不应该覆盖init方法,你不应该在代码中分配init。如果这样做,您在界面构建器中创建的视图将无法加载。
所以不要使用alloc init,而是按照我指出的答案做一些类似的事情。
无论如何,我无法看到你在哪里初始化
@property (strong, nonatomic) UIView *view;
不是IBOutlet并且你从不在任何地方启动它,所以显然总是nil