我有我的ViewController正在顯示的Xib UIView。 Xib包含一個UILabel和一個UIButton。我的按鈕塗在我的xib上,我使用它來瀏覽我的SecondViewController,並通過委託方法實現這一點。如何從UIViewController更改Xib的標籤
這是關於我的標籤的事情;因爲我的按鈕是透明的,我可以在按鈕下方顯示它。我不能做的是從ViewController中更改mylabel的文本。
我做了一些搜索和遇到的建議是這樣的:
是創建子視圖另一個筆尖文件,並把該子視圖中 那裏。然後在那個.nib文件中,使文件擁有者IOSubview。物業 連接將工作得很好。然後以編程方式將子視圖添加到您的IOViewController的 。請記住首先從bundle加載nib 文件。
但它沒有任何意義對我來說,因爲我創造了廈門國際銀行在第一個原因是使用它不止一次。我相信解決這個問題可能會簡單得多。但是怎麼樣?
這裏是一個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
視圖控制器。 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
您需要創建的UILabel注入到你的UIView類,在 「微型」 類你的案子。 –