2014-09-12 172 views
0

我试图在满足特定条件时将笔尖装入视图。我能够在故事板上显示笔尖,认为我无法在按下按钮时执行任何操作。 只是为了表达我的最终目标。我想用我的nib视图创建一种模板,然后从我的故事板显示nib视图,但使用故事板发送的值操纵nib视图中的标签。 好的,我会尽我所能尝试并展示我的细节步骤,以便我尝试完成此操作。 首先这里是我的项目的图片。显示笔尖上方的故事板视图

enter image description here

我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 感谢提前的家伙。

回答

1

你从来没有设置您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; 

注意,您可以使用自动调整,即使你的故事板使用了自动布局的限制。 Autolayout会将自动调整屏幕变成您的限制条件。

或者您可以将您的VCDemoView作为根视图的子视图放置在故事板中,并在其中设置约束。

+0

这是令人困惑的,因为尽管我没有设置大小,我仍然能够在我的viewcontroller上看到它。如果你不介意,还有一个问题。我如何将笔尖的高度改变为可能像父母的50%? – Miguel 2014-09-12 20:08:30

+0

我不确定我是否明白您想要更改的内容。 – 2014-09-12 20:16:01

+0

我基本上只是想将高度和宽度设置为小于故事板的百分比,因为缺乏更好的术语,我将从多个故事板中收集此视图以供使用,但我希望更改它的尺寸,以便不占用整个视图的宽度和高度。希望这是有道理的。 – Miguel 2014-09-12 21:54:16

1

你不应该打电话和在内动作查看这是什么控制器应该处理。 您需要将fileOwner更改为控制器,并将View Class更改为VCDemoView,将视图连接到IB中的控制器,然后从那里加载该笔尖,然后将其作为子视图添加到ViewController的视图中。

这个编辑的代码,,我希望它有助于.. https://dl.dropboxusercontent.com/u/33359624/demo-SO/Archive.zip

1

您未设置CustomView框架,因此默认框架为CGRectZero。如果你设置CustomView的框架,它会正常工作。

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    VCDemoView * CustomView = [[VCDemoView alloc]init] ; 
    CustomView.frame =self.view.frame; 
    [self.view addSubview:CustomView]; 



} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end