2014-09-12 173 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