在多个视图控制器中使用代理 [英] Using Delegates in multiple view controllers

查看:161
本文介绍了在多个视图控制器中使用代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多个视图控制器,并使用委托将一个视图控制器中的UITextfield链接到另一个视图控制器中的标签。



ViewController.h

  #import< UIKit / UIKit.h> 

@interface ViewController:UIViewController< UITextFieldDelegate>
{
IBOutlet UITextField * txtField;

}

@end

ViewController。 m

  #importViewController.h
#importViewController2nd.h


@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
//加载视图后,通常从笔尖进行任何其他设置。
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}
- (BOOL)textFieldShouldReturn :( UITextField *)textField {

lbl.text = [NSString stringWithFormat:@Hello,%@,txtField.text]
[txtField resignFirstResponder];
返回YES;
}

@end

ViewController2nd.h

  #import< UIKit / UIKit.h> 

@interface ViewController2nd:UIViewController< UITextFieldDelegate> {

IBOutlet UILabel * lbl;
}

@end

ViewController2nd.m

  #importViewController2nd.h

@interface ViewController2nd()

@ end

@implementation ViewController2nd

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
//自定义初始化
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
//从其笔尖加载视图后,请执行任何其他设置。
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

@end

我得到的错误是:



在Viewcontroller.m中使用未声明的标识符lbl

不知道如何解决这个问题。需要一些指导..谢谢...

解决方案

您没有范围/超出 ViewController2nd IBOutlet UILabel * lbl; ViewController 中,您需要自定义代理 ViewController 代理 ViewController2nd 并传回数据。有关详细信息,请查看此帖


I am using multiple view controllers and trying the link the UITextfield in one view controller to the label in another view controller using delegate.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField *txtField;

}

@end

ViewController.m

#import "ViewController.h"
#import "ViewController2nd.h"


@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    lbl.text = [NSString stringWithFormat:@"Hello, %@",txtField.text]
    [txtField resignFirstResponder];
    return YES;
}

@end

ViewController2nd.h

#import <UIKit/UIKit.h>

@interface ViewController2nd : UIViewController <UITextFieldDelegate> {

    IBOutlet UILabel *lbl;
}

@end

ViewController2nd.m

#import "ViewController2nd.h"

@interface ViewController2nd ()

@end

@implementation ViewController2nd

- (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)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

The error i get is:

Use of undeclared identifier lbl in Viewcontroller.m

Not sure how to solve this problem. Need some guidance.. Thanks...

解决方案

You don't have scope/excess of ViewController2nd's IBOutlet UILabel *lbl; in ViewController for this you need custom delegates, a ViewController a delegate of ViewController2nd and pass data back. Have a look at this post for more details.

这篇关于在多个视图控制器中使用代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆