在多个视图控制器中使用代理 [英] Using Delegates in multiple view controllers
问题描述
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屋!