[英]Display nib above of my storyboard view
我正在嘗試在滿足特定條件時將筆尖加載到視圖中。 我可以在故事板上顯示筆尖,以為按下按鈕時似乎無法執行任何操作。 只是為了表達我的最終目標。 我想用筆尖視圖創建某種模板,然后從情節提要中顯示筆尖視圖,但要使用情節提要中發送的值在筆尖視圖中操作標簽。 好的,所以我將盡我最大的努力來展示我如何實現這一目標的詳細步驟。 首先,這是我的項目的圖片。
我的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謝謝各位。
您不應該在View內部調用和操作Controller應該處理的內容。 您需要將fileOwner更改為控制器,將View類更改為VCDemoView ,將View連接到IB中的控制器,然后從其中加載筆尖,然后將其作為subView添加到ViewController的視圖中。
這是編輯后的代碼,希望對您有所幫助。https://dl.dropboxusercontent.com/u/33359624/demo-SO/Archive.zip
您從未設置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
作為根視圖的子VCDemoView
放在情節VCDemoView
中,並在其中設置約束。
您沒有設置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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.