2017-08-03 72 views
0

我有我的ViewController正在显示的Xib UIView。 Xib包含一个UILabel和一个UIButton。我的按钮涂在我的xib上,我使用它来浏览我的SecondViewController,并通过委托方法实现这一点。如何从UIViewController更改Xib的标签

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

我做了一些搜索和遇到的建议是这样的:

是创建子视图另一个笔尖文件,并把该子视图中 那里。然后在那个.nib文件中,使文件拥有者IOSubview。物业 连接将工作得很好。然后以编程方式将子视图添加到您的IOViewController的 。请记住首先从bundle加载nib 文件。

链接:https://stackoverflow.com/a/20294118/1450201

但它没有任何意义对我来说,因为我创造了厦门国际银行在第一个原因是使用它不止一次。我相信解决这个问题可能会简单得多。但是怎么样?

这是我的厦门国际银行的样子: enter image description here

这里是一个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 
+0

您需要创建的UILabel注入到你的UIView类,在 “微型” 类你的案子。 –

回答

2

更新您的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 

和视图控制器

- (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 
} 
+0

谢谢!我会在5分钟后将其标记为答案。规则符合如此 –