【问题标题】:Change label text color using delegate使用委托更改标签文本颜色
【发布时间】:2012-11-19 12:41:16
【问题描述】:

我有两个视图控制器,ParentViewController 有使用 SecondViewController 更新的标签。 现在我想从ChildViewController 更改ParentViewController 的标签文本颜色,因为我正在使用协议和委托,但它显示错误。

我的代码是这样的:

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

更新

这就是我创建标签的方式

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

}

【问题讨论】:

  • 您必须创建 childViewController 的实例才能访问 deleagte proeprty。
  • 你的dismissModalViewControllerAnimated 有用吗?

标签: iphone ios delegates protocols


【解决方案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

【讨论】:

  • 解决了这个问题,但我的 updateLabel 仍然没有被称为任何想法,为什么?
  • 我需要更多代码。您能否更新您的问题并粘贴更多代码,例如您如何定义alabel?你如何展示你的childViewController?以此类推。
  • 你能把你的项目放在某个地方并给我链接吗?我仍然找不到这些代码的问题。
【解决方案2】:

您没有设置ChildViewController 的委托属性。您将其设置为ParentViewController。但是ParentViewController 没有那个属性。 在ParentViewController中设置ChildViewController's对象的委托属性

ChildViewController.delegate=self

【讨论】:

  • 抱歉,我的问题只是输入错误,但现在查看,仍然出现同样的错误。
  • 然后检查上面的 sunkehappy 答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 2022-08-08
  • 2020-09-30
  • 1970-01-01
相关资源
最近更新 更多