0

メイン ビューには、ラベルとボタンがあります。UILabel とボタンを含むサブビューを 1 つ追加しました。私がやりたいことは、2 つのボタンのうちの 1 つを押して、両方のビューで 2 つのラベルを更新することです。問題は、サブビューのボタンをクリックすると、サブビューのラベルのみが更新されることです。逆に、メイン ビューのラベルは、メイン ビューのボタンをクリックしたときにのみ変更されます。

他のビューの各 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.サブビュー.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

私はあなたがやろうとしていることを手に入れたと思います、そしてここにあなたがすべきことがあります:

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

上記のコードでは、自己はViewControllerに反映されます

于 2012-08-28T02:54:02.303 に答える
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
于 2012-08-28T03:09:48.180 に答える