私はViewControllerで表示されているXib UIViewを持っています。 XibにはUILabelとUIButtonが含まれています。私のボタンは私のxibの上を覆い、私はSecondViewControllerをナビゲートするためにこのボタンを使用しています。UIViewControllerからXibのラベルを変更するには
ここに私のラベルに関するものがあります。私のボタンは透明なので、ボタンの下に表示することができます。私ができないことは、MylabelのテキストをViewControllerから変更することです。
私はいくつかの検索を行なったし、このような提案に遭遇:
はサブビューのための別の.nibファイルを作成し、そこ にサブビューを入れています。その.nibファイルで、ファイル所有者IOSubviewを作成します。プロパティ 接続は正常に動作します。次に、IOViewControllerをプログラマチックに に追加するだけです。ちょうど束から最初の ファイルをロードすることを忘れないでください。
が、私は最初にXIBを作成した理由は複数回、それを使用することですので、それは私には意味がありません。私はこの問題に対する解決策がはるかに単純だと信じています。しかし、どのように?
そしてここでGitHubのレポリンクと私のコードです:
https://github.com/TimurAykutYildirim/demoView
ViewController.h
#import <UIKit/UIKit.h>
#import "Mini.h"
@interface ViewController : UIViewController <SelectionProtocol>
@property (weak, nonatomic) IBOutlet Mini *miniView;
@property (weak, nonatomic) IBOutlet UILabel *miniLabel;
@end
のViewController
これは私XIBがどのように見えるかです。 m
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.miniView.delegate = self;
}
-(void) isClicked {
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Mini.h
#import <UIKit/UIKit.h>
@protocol SelectionProtocol;
@interface Mini : UIView
@property (nonatomic, weak) id<SelectionProtocol> delegate;
- (IBAction)btnClick:(id)sender;
@end
@protocol SelectionProtocol <NSObject>
@required
-(void) isClicked;
@end
Mini.m
#import "Mini.h"
@implementation Mini
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self load];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self load];
}
return self;
}
- (void)load {
UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"Mini" owner:self options:nil] firstObject];
[self addSubview:view];
view.frame = self.bounds;
// ui component properties will be set here
}
- (IBAction)btnClick:(id)sender {
if ([self.delegate conformsToProtocol:@protocol(SelectionProtocol)]) {
[self.delegate isClicked];
}
}
@end
あなたは "ミニ" クラスで、あなたのケースで、あなたのUIViewクラスにUILabelの注入を作成する必要があります。 –