Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

iphone - Change label text color using delegate

I have two view controller, ParentViewController have label that updated using SecondViewController. Now I want to change ParentViewController's label text color from ChildViewController, for that i am using protocol and delegate but it shows error.

My code is like this:

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

this is how i am creating label

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

}

And when user tap label then

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


    [self presentModalViewController:childViewController animated:NO];

}

and in ChildViewCoontroller when user click done button then

-(IBAction)doneBtn:(id)sender
{

    [adelegate updateLabel];

     [self dismissModalViewControllerAnimated:NO];

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...