使用代理更改标签文本颜色
我有两个视图控制器,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];
}
回答:
#import <UIKit/UIKit.h> #import "childViewController.h"
@interface ParentViewController : UIViewController<UserResizableViewDelegate,ViewControllerDelegate>
@property (span, 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
回答:
你不设置的ChildViewController
委托财产。您正在设置它为ParentViewController
。但ParentViewController
没有该属性。在ParentViewController
ChildViewController.delegate=self
以上是 使用代理更改标签文本颜色 的全部内容, 来源链接: utcz.com/qa/257101.html