0

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

2 回答 2

0

我认为你需要阅读《Objective-C 编程语言》”并了解“对象”和“指针”的含义

这是代码

子视图.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

子视图.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

视图控制器.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
于 2012-08-28T03:09:48.180 回答
0

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

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

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

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

4-打开 SubView.xib 并将 SubView(对象树中的根节点)与您在步骤 3 中创建的文件所有者 IBOutlet 挂钩

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

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

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

于 2012-08-28T02:54:02.303 回答