如何从 UIViewController 更改 Xib 的标签

How to change Xib's label from UIViewController

我的 ViewController 正在显示 Xib UIView。 Xib 包含一个 UILabel 和一个 UIButton。我的按钮覆盖在我的 xib 上,我用它来导航我的 SecondViewController,我通过委托方法实现了这一点。

这是关于我的标签的事情;因为我的按钮是透明的,所以我可以在按钮下方显示它。我不能做的是将 mylabel 的文本从 ViewController.

更改为

我做了一些搜索,发现了这样的建议:

is create another .nib file for the subview and put the subview in there. Then in that .nib file, make the file owner IOSubview. Property connections will work just fine there. Then just add the subview to your IOViewController programatically. Just remember to load the nib file from bundle first.

link :

但这对我来说没有意义,因为我最初创建 xib 的原因是多次使用它。我相信解决这个问题可能会简单得多。但是怎么办呢??

这是我的 xib 的样子:

这是一个 github 回购 link 和我的代码:

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.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

更新您的 Mini.h 以向其添加标签插座。

Mini.h

#import <UIKit/UIKit.h>

@protocol SelectionProtocol;

@interface Mini : UIView

@property (nonatomic, weak) id<SelectionProtocol> delegate;

@property (weak, nonatomic) IBOutlet UILabel *miniLabel;

- (IBAction)btnClick:(id)sender;
@end


@protocol SelectionProtocol <NSObject>

@required
-(void) isClicked;

@end

并在 ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.miniView.delegate = self;
    self.miniView.miniLabel.text = //set whatever value
}