2012-08-28 107 views
0

主視圖有一個標籤和一個按鈕。我添加了一個包含UILabel和一個按鈕的子視圖。我想要做的是,我想通過按下兩個按鈕中的一個來更新兩個視圖中的兩個標籤。 問題是,如果我點擊子視圖的按鈕,只會更新子視圖的標籤。相反,只有當我點擊主視圖中的按鈕時,主視圖的標籤纔會更改。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 

回答

0

我想想我得到了你想要做的和他re是你應該做的:

1-創建3個新文件(xib,m,h)稱它們爲SubView.xib,m,h。你必須確保它繼承UIView。

2-在xib文件中繪製子視圖元素並使用它鉤住IBOutlets。但要確保該文件所有者是的ViewController和根視圖類類型爲「子視圖」

3-制式子視圖的一個IBOutlet在視圖控制器

4-開放SubView.xib和勾子視圖(中 該行的IBOutlet中的視圖控制器後,將有子視圖的對象:在對象樹與文件所有者IBOutlet中所做@步3

加載在ViewController.m新的子視圖只需撥打該行根節點) 。

[[NSBundle mainBundle] loadNibNamed:@"SubView" owner:self options:nil]; 
在上面的代碼中自行

被反映到ViewController

+0

感謝您輸入的意見。這很有幫助。 – user1629100

0

我覺得你需要閱讀「The Objective-C Programming Language」,知道「對象」和「指針」如何意味着

下面是代碼

SubView.h

​​3210

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 
+0

感謝您的建議。這有助於解決我的問題。 – user1629100