我正在尝试在满足特定条件时将笔尖加载到视图中。我可以在故事板上显示笔尖,以为按下按钮时似乎无法执行任何操作。
只是为了表达我的最终目标。我想用笔尖视图创建某种模板,然后从情节提要中显示笔尖视图,但要使用从情节提要中发送的值在笔尖视图中操纵标签。
好的,所以我会尽力尝试显示我如何完成此操作的详细步骤。
首先,这是我的项目的图片。
我的VCDemoView.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
VCDemoView.m
#import "VCDemoView.h"
@interface VCDemoView ()
@property(nonatomic) IBOutlet UIImageView *imageView;
@property(nonatomic) IBOutlet UILabel *titleLabel;
@property(nonatomic) IBOutlet UILabel *subtitleLabel;
@property(nonatomic) IBOutlet UIView *container;
//- (IBAction)changeLabel:(id)sender;
@end
@implementation VCDemoView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(void)initalizeSubviews
{
//Load the contents of the nib
NSString *nibName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[nib instantiateWithOwner:self options:nil];
//Add the view loaded from the nib into self.
[self addSubview:self.container];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)changeLabel:(id)sender {
//[self.subtitleLabel.text = @"MIGUEL"];
_subtitleLabel.text = @"miguel";
}
@end
故事板视图控制器。
#import "ViewController.h"
#import "VCDemoView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
[self.view addSubview:CustomView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
谁能发现我在做什么错?我似乎无法与按钮进行通讯?我敢肯定这是愚蠢的。
如果您希望以这种方式查看它,那么这里是带有所有源代码的dropbox项目的链接。
https://www.dropbox.com/sh/23cbhs4qvsxwei0/AADFs6MN3eqJNK42Io2etuJea?dl=0
谢谢你们。
最佳答案
您从未设置VCDemoView
的框架。框架默认为CGRectZero
。
视图默认情况下不会裁剪其子视图,因此,即使它们在VCDemoView
的范围之外,您仍然可以看到标签和按钮。但是视图不会在其范围之外接收事件,因此,当您点击按钮时,顶级视图会吞下该事件,因为它发现该事件不在其一个子视图(VCDemoView
)的范围之内。
您可以像这样固定VCDemoView
的框架:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
CustomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CustomView.frame = self.view.bounds;
请注意,即使情节提要板使用自动布局约束,也可以使用自动调整大小。自动布局会将自动调整大小的遮罩变成您的约束。
或者,您可以将
VCDemoView
作为根视图的子级放在情节提要中,并在那里设置约束。关于ios - 在 Storyboard View 上方显示 Nib ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25814728/