在我的故事板视图上方显示nib

时间:2014-09-12 18:39:53

标签: ios objective-c iphone

我正在尝试在满足特定条件时将笔尖加载到视图中。我能够在我的故事板上显示笔尖,以为我无法在按下按钮时执行任何操作。 只是为了表达我的最终目标。我想用我的笔尖视图创建一个模板,然后从我的故事板中显示笔尖视图,但是使用从故事板发送的值来操纵我的笔尖视图中的标签。 好的,所以我会尽力尝试展示我如何努力实现这一目标的详细步骤。 首先是我的项目图片。

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 谢谢是先进的人。

3 个答案:

答案 0 :(得分:1)

你不应该在控制器应该处理的 View 中调用和操作。 您需要将fileOwner更改为控制器,将View Class更改为 VCDemoView ,将View连接到IB中的控制器,然后从那里加载nib,然后将其作为子视图添加到ViewController的视图。

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

答案 1 :(得分: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作为根视图的子项放在故事板中,并在那里设置约束。

答案 2 :(得分: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