主视图有一个标签和一个按钮。我添加了一个包含 UILabel 和按钮的子视图。我想要做的是我想通过按下两个按钮之一来更新两个视图中的两个标签。问题是,如果我点击子视图的按钮,子视图的标签只会更新。相反,只有当我单击主视图中的按钮时,主视图的标签才会更改。
我想知道如何更新另一个视图的每个 label.text 。
提前致谢...
这是我的代码。1.ViewController.h
#import <UIKit/UIKit.h>
#import "SubView.h"
@interface ViewController : UIViewController{
SubView *subView;
}
@property (weak, nonatomic) SubView *subView;
@property (retain, nonatomic) IBOutlet UILabel *amount;
@end
2.ViewController.m
#import "ViewController.h"
#import "SubView.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize subView=_subView;
@synthesize amount;
- (void)viewDidLoad
{
[super viewDidLoad];
subView = [[SubView alloc]initWithFrame:CGRectMake(10, 10, 300, 600)];
[self.view addSubview:subView];
amount = [[UILabel alloc]initWithFrame:CGRectMake(400, 400, 100, 50)];
amount.text = @"test in the main view";
[self.view addSubview:amount];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(400, 300, 40, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:@selector(labelChange:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; // add a button in the main view
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)labelChange:(id)sender{
amount.text = @"defaul label";
SubView *sv = [[SubView alloc]init];
sv.addObject2.text = @"label in the subview";
NSLog(@"sv.addObject2.text = %@",sv.addObject2);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
3.子视图.h
#import <UIKit/UIKit.h>
@interface SubView : UIView {
UIButton *btn;
UILabel *label;
}
//@property (strong, nonatomic) UIView *view;
@property (weak, nonatomic) UIButton *btn;
@property (weak, nonatomic) UILabel *label;
- (UIButton *)addObject1;
- (UILabel *)addObject2;
@end
4.子视图.m
#import "SubView.h"
#import "ViewController.h"
@implementation SubView
@synthesize btn=_btn, label=_label;
//@synthesize view;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
//UIView *contentView = [[UIView alloc]initWithFrame:frame];
self.backgroundColor = [UIColor blackColor];
UIButton *object1 = [self addObject1];
UILabel *object2 = [self addObject2];
[self addSubview:object1];
[self addSubview:object2];
return self;
}
- (UIButton *)addObject1{
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 100, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:@selector(labelChange:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (IBAction)labelChange:(id)sender{
label.text = @"change the label in the subview";
ViewController *vc = [[ViewController alloc]init];
[vc.amount setText:@"change the label in the mainview"];
}
- (UILabel *)addObject2{
label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 50)];
[label setText: @"default"];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:@"Arial" size:12];
label.textColor = [UIColor whiteColor];
return label;
}
@end