Objective c UILabel文本不更新
主视图有一个标签和一个按钮。我添加了一个子视图,其中包含一个UILabel和一个按钮。我想做的是,我想通过按下两个按钮中的一个按钮来更新两个视图中的两个标签。 问题是,如果单击子视图的按钮,子视图的标签只会更新。相反,只有在单击主视图中的按钮时,主视图的标签才会更改 我想知道如何更新其他视图的每个label.text 先谢谢你 这是我的密码。 1.ViewController.hObjective c UILabel文本不更新,objective-c,xcode,uilabel,subview,updates,Objective C,Xcode,Uilabel,Subview,Updates,主视图有一个标签和一个按钮。我添加了一个子视图,其中包含一个UILabel和一个按钮。我想做的是,我想通过按下两个按钮中的一个按钮来更新两个视图中的两个标签。 问题是,如果单击子视图的按钮,子视图的标签只会更新。相反,只有在单击主视图中的按钮时,主视图的标签才会更改 我想知道如何更新其他视图的每个label.text 先谢谢你 这是我的密码。 1.ViewController.h #import <UIKit/UIKit.h> #import "SubView.h" @interf
#import <UIKit/UIKit.h>
#import "SubView.h"
@interface ViewController : UIViewController{
SubView *subView;
}
@property (weak, nonatomic) SubView *subView;
@property (retain, nonatomic) IBOutlet UILabel *amount;
@end
三,。子视图.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
我想我明白了你想要做的,以下是你应该做的: 1-创建3个新文件(xib,m,h),称它们为SubView.xib,m,h。您必须确保它继承UIView 2-在xib文件中,绘制子视图元素并用它钩住IBO。但请确保文件所有者是ViewController,根视图类类型是“SubView” 3-在ViewController中创建子视图类型的IBOutlet 4-打开SubView.xib并将子视图(对象树中的根节点)与您在步骤3中创建的文件所有者IBOutlet挂钩 要在ViewController.m中加载新的子视图,只需调用以下行: 在该行之后,ViewController中的IBOutlet将具有子视图对象
[[NSBundle mainBundle] loadNibNamed:@"SubView" owner:self options:nil];
在上面的代码中,self会反映到ViewController中,我认为您需要阅读“”并了解“Object”和“Pointer”的含义
这是密码
子视图.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
#import <UIKit/UIKit.h>
@interface SubView : UIView {
UIButton *btn;
UILabel *label;
}
@property (weak, nonatomic) UIViewController *vc;
@property (weak, nonatomic) UIButton *btn;
@property (weak, nonatomic) UILabel *label;
- (UIButton *)addObject1;
- (UILabel *)addObject2;
@end
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
#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)];
subView.vc = self;
[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
}
- (IBAction)labelChange:(id)sender{
amount.text = @"defaul label";
subview.addObject2.text = @"label in the subview";
}
@end
谢谢你的评论。很有帮助。谢谢你的推荐。这有助于解决我的问题。
#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)];
subView.vc = self;
[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
}
- (IBAction)labelChange:(id)sender{
amount.text = @"defaul label";
subview.addObject2.text = @"label in the subview";
}
@end