2012-11-19 27 views
-1

我有兩個視圖控制器,ParentViewController的標籤使用SecondViewController更新。 現在我想更改ParentViewController的標籤文本顏色從ChildViewController,因爲我使用協議和委託,但它顯示錯誤。使用代理更改標籤文本顏色

我的代碼是這樣的:

ChildViewController.h

@protocol ViewControllerDelegate 

-(void) updateLabel; 

@end 
@interface ChildViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate> 
{ 
    id <ViewControllerDelegate> delegate; 
} 

@property (assign) id <ViewControllerDelegate> delegate; 
@end 

ChildViewController.m

@implementation childViewController 

@synthesize delegate; 

- (IBAction)backBtn:(id)sender { 
    [delegate updateLabel]; 
} 
@end 

ParentViewController.h

#import <UIKit/UIKit.h> 
#import "childViewController.h" 

@interface ParentViewController : UIViewController<UserResizableViewDelegate,ViewControllerDelegate> 

@end 

ParentViewController.m

@implementation ParentViewController 

- (void)viewDidLoad 
{ 
    ChildViewController.delegate=self; //here it show error that ChildViewController does not have property delegate 
} 
- (void)updateLabel 
{ 
    alabel.textColor = [UIColor blueColor]; 
} 

@end 

UPDATE

我這是怎麼創建標籤

ParentViewController.m

- (void)viewDidLoad 
{ 
CGRect imageFrame = CGRectMake(10, 10, 150, 80); 
    labelResizableView = [[UserResizableView alloc] initWithFrame:imageFrame]; 
    alabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)]; 

    alabel.text = @"Drag me!"; 
    //alabel.text = self.newsAsset.title; 
    alabel.adjustsFontSizeToFitWidth = NO; 
    alabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    alabel.font = [UIFont boldSystemFontOfSize:18.0]; 
    //alabel.textColor = [UIColor blueColor];  
    // alabel.shadowColor = [UIColor whiteColor]; 
    // alabel.shadowOffset = CGSizeMake(0, 1); 
    alabel.backgroundColor = [UIColor clearColor]; 
    alabel.lineBreakMode = UILineBreakModeWordWrap; 
    alabel.numberOfLines = 10; 
    alabel.minimumFontSize = 8.; 
    alabel.adjustsFontSizeToFitWidth = YES; 
    [alabel sizeToFit]; 
    labelResizableView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 



    // enable touch delivery 
    alabel.userInteractionEnabled = YES; 

    UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)]; 
    doubleTap.numberOfTapsRequired = 2; 
    [self.view addGestureRecognizer:doubleTap]; 

    //Calculate the expected size based on the font and linebreak mode of your label 
    CGSize maximumLabelSize = CGSizeMake(296,9999); 

    CGSize expectedLabelSize = [myString sizeWithFont:alabel.font 
            constrainedToSize:maximumLabelSize 
             lineBreakMode:alabel.lineBreakMode]; 


    //adjust the label the the new height. 
    CGRect newFrame = alabel.frame; 
    newFrame.size.height = expectedLabelSize.height+40; 
    newFrame.size.width = expectedLabelSize.width+40; 

    alabel.frame = newFrame; 
    labelResizableView.frame = newFrame; 

    labelResizableView.contentView = alabel; 
    labelResizableView.delegate = self; 
    [self.view addSubview:labelResizableView]; 

} 

當用戶水龍頭標籤然後

- (void)labelTap:(UITapGestureRecognizer *)tapGesture { 
    ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:[NSBundle mainBundle]]; 


    [self presentModalViewController:childViewController animated:NO]; 

} 

和ChildViewCoontroller當用戶點擊完成按鈕,然後

-(IBAction)doneBtn:(id)sender 
{ 

    [adelegate updateLabel]; 

    [self dismissModalViewControllerAnimated:NO]; 

} 
+1

ChildViewController's對象的 設置委託財產你必須創建childViewController的實例來訪問deleagte廣告載體。 –

+0

你的'dismissModalViewControllerAnimated'工作嗎? – sunkehappy

+0

是的,它確實有效。 –

回答

1
#import <UIKit/UIKit.h> 
#import "childViewController.h" 

@interface ParentViewController : UIViewController<UserResizableViewDelegate,ViewControllerDelegate> 

@property (strong, nonatomic) ChildViewController *childViewController; 

@end 

@implementation ParentViewController 

- (void)viewDidLoad 
{ 
    self.childViewController = [[ChildViewController alloc] init]; 
    childViewController.delegate=self; //here it show error that ChildViewController does not have property delegate 
} 
@end 
+0

上面的答案,解決了這個問題,但仍然我的updateLabel不被稱爲任何想法爲什麼? –

+0

我需要更多的代碼。你能否更新你的問題並粘貼更多的代碼,比如你如何定義你的'alabel'?你如何顯示你的'childViewController'?等等。 – sunkehappy

+0

請檢查我的更新。 –

0

你不設置的ChildViewController委託財產。您正在設置它爲ParentViewController。但ParentViewController沒有該屬性。在ParentViewController

ChildViewController.delegate=self 
+0

對不起,它只是在我的問題中輸入錯誤,但現在看到,仍然有相同的錯誤。 –

+0

然後檢查sunkehappy –