UILabel Text不会更新

时间:2012-08-28 02:34:41

标签: objective-c xcode uilabel subview updates

主视图有标签和按钮。我添加了一个包含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。 SubView.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。 SubView.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

2 个答案:

答案 0 :(得分:0)

我想我得到了你想做的事情,这就是你应该做的事情:

1-创建3个新文件(xib,m,h)称它们为SubView.xib,m,h。你必须确保它继承了UIView。

xib文件中的

2-绘制子视图元素并用它挂钩IBOutlets。但请确保文件所有者是ViewController,根视图类类型是“SubView”

3-在ViewController中创建一个Subview类型的IBOutlet

4-打开SubView.xib并使用您创建的文件所有者IBOutlet挂钩SubView(对象树中的根节点)@ step 3

在ViewController.m中加载新的SubView只需调用以下行: 在该行之后,ViewController中的IBOutlet将具有SubView的对象。

[[NSBundle mainBundle] loadNibNamed:@"SubView" owner:self options:nil];

在上面的代码中,self被反映到ViewController

答案 1 :(得分:0)

我想你需要阅读“The Objective-C Programming Language”并知道“对象”和“指针”的含义

这是代码

SubView.h

#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

Subview.m

#import "SubView.h"
#import "ViewController.h"

@implementation SubView
@synthesize btn=_btn, label=_label;
@synthesize vc;

- (id)initWithFrame:(CGRect)frame
{
    self = [super 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";
    [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

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)];
    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