使用委托更改子视图中的UILabel文本

时间:2014-02-03 20:52:57

标签: ios iphone objective-c delegates

我有一个带有标签的视图。该视图的下方填充了一个子视图,该子视图具有自己的视图控制器。这个子视图有一个按钮。我的目标:我想通过按子视图中的按钮来更改标签。

我已经尝试了下面的代码。当我让委托的ViewWillAppear方法调用SubviewController的'doSomeTask'方法时,它工作正常。计时器开始计数,几秒钟后,SubviewController调用委托changeLabel方法,并更改标签。但是,当我通过按下按钮手动调用'doSomeTask'方法时,委托不会监听。该调用未达到changeLabel方法。

可能我在这里做了一些荒谬的事。原谅我,我只是在学习iOS编程。提前感谢所有反馈。

MyViewController.h

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

@interface MyViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIView *detailView;
@property (strong, nonatomic) IBOutlet UILabel *theLabel;

- (IBAction)changeLabel:(id)sender;
@end

MyViewController.m

#import "MyViewController.h"
#import "MySubViewcontroller.h"

@interface MyViewController ()
@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    MySubViewController *subVC = [[MySubViewController alloc] init];
    [self addChildViewController:subVC];
    [self.detailView addSubview:subVC.view];
    // [subVC setDelegate:self]; (no success trying this)
    [subVC didMoveToParentViewController:self];  
}

-(void)viewWillAppear:(BOOL)animated
{
    MySubViewController *mySubVC  = [[MySubViewController alloc] init];
    [mySubVC setDelegate:self];
    [mySubVC doSomeTask]; // this works fine
}

// delegate method
- (void)changeLabel{
    self.theLabel.text = @"Hello!";
}

MySubviewController.h

#import <UIKit/UIKit.h>

@protocol MySubViewControllerDelegate <NSObject>
@required
- (void)changeLabel;
@end


@interface MySubViewController : UIViewController

@property (nonatomic, assign) id delegate;

- (IBAction)changeButton:(id)sender;
- (void)doSomeTask;
- (void)goChangeLabel;

@end

MySubViewController.m

#import "MySubViewController.h"

@interface MySubViewController ()

@end

@implementation MySubViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)goChangeLabel
{
    [[self delegate] changeLabel];
}

- (void)doSomeTask
{
    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(goChangeLabel) userInfo:nil repeats:NO];   
}

- (IBAction)changeButton:(id)sender {
    //  [self doSomeTask];    (no succes trying this)
}

@end

1 个答案:

答案 0 :(得分:0)

你的问题是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    MySubViewController *subVC = [[MySubViewController alloc] init];
    [self addChildViewController:subVC];
    [self.detailView addSubview:subVC.view];
    // [subVC setDelegate:self]; (no success trying this)
    [subVC didMoveToParentViewController:self];  
}

-(void)viewWillAppear:(BOOL)animated
{
    MySubViewController *mySubVC  = [[MySubViewController alloc] init];
    [mySubVC setDelegate:self];
    [mySubVC doSomeTask]; // this works fine
}

在这两种方法中,您创建了两个类型为MySubViewController的独立变量。您只在viewWillAppear内创建的变量上设置了委托。

这些不是静态类,单例或其他类似的东西。如果您创建一个类型的变量,它绝不会链接到另一个。您可以使用相同的方式创建彼此无关的多个NSString

像这样创建:

@implementation MyViewController
{
    MySubViewController *_subVC;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    _subVC = [[MySubViewController alloc] init];
}

现在您可以在该文件中的任何位置使用_subVC作为一个参考