use*_*285 5 objective-c uiview ios
我绝望地被困住了,我会很感激指针.
我们正在尝试构建一个包含多个视图的视图控制器,这些视图是UIView的子类.除了我们需要手动初始化视图或再次手动调用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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
然后我们从视图控制器初始化视图:
introView *view = [[introView alloc]init];
view.title = @"Test";
[self addSubview:view];
Run Code Online (Sandbox Code Playgroud)
这是问题 - 通过调用此视图,视图显示正确但标题为NULL;
[43605:1957090] title: (null)
Run Code Online (Sandbox Code Playgroud)
但是,如果我们再次调用awakeFromNib,则视图会正确初始化
在视图控制器中:
introView *view = [[introView alloc]init];
view.title = @"Test;
[self addSubview:view];
[view awakeFromNib];
Run Code Online (Sandbox Code Playgroud)
然后它工作:
2014-11-21 09:33:03.500 Test[43706:1972060] title: (null)
2014-11-21 09:33:03.500 Test[43706:1972060] title: Test
Run Code Online (Sandbox Code Playgroud)
或者,如果我将initialize方法设为public并在初始化后调用它也可以.但这在我眼中打败了目的......
在视图控制器中:
introView *view = [[introView alloc]init];
view.title = @"Test;
[self addSubview:view];
[view initialize]; //calling the method directly...
Run Code Online (Sandbox Code Playgroud)
在我看来,我们以某种方式运行到视图还没有准备好的情况但是它在第二次尝试时工作(即使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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
查看控制器类
//
// 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
Run Code Online (Sandbox Code Playgroud)
-------输出-----
2014-11-21 11:49:38.893 CustomView[45653:2123668] LabelMadness.initWithFrame:
2014-11-21 11:49:38.893 CustomView[45653:2123668] (null)
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
您根本不需要使用 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;
}
Run Code Online (Sandbox Code Playgroud)
要设置标题,您可以覆盖标题属性的设置器,
-(void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = title;
}
Run Code Online (Sandbox Code Playgroud)
控制器中的代码将与您最初尝试的代码相同。
| 归档时间: |
|
| 查看次数: |
9037 次 |
| 最近记录: |