我正在使用多个视图控制器并尝试使用委托将一个视图控制器中的UITextfield链接到另一个视图控制器中的标签。
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
我得到的错误是:
在Viewcontroller.m中使用未声明的标识符lbl
不确定如何解决这个问题。需要一些指导..谢谢...
答案 0 :(得分:1)
ViewController2nd
中IBOutlet UILabel *lbl;
ViewController
的范围/超出范围,因为您需要自定义代理,ViewController
代理ViewController2nd
并传回数据。有关详细信息,请查看this post。
答案 1 :(得分:0)
lbl
的声明是在错误的类中完成的。发布IBOutlet UILabel *lbl;
在viewcontroller.h
如果你想使用其他类的变量将代码更改为
#import <UIKit/UIKit.h>
#import "ViewController2.h"
@interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *txtField;
ViewController2 *viewController2;
}
@property(nonatomic,retain)ViewController2 *viewController2;
@end
ViewController.m
#import "ViewController.h"
#import "ViewController2nd.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize viewcontroller2;
- (void)viewDidLoad
{
viewController2 = [[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];
[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{
viewController2.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;
}
@property(nonatomic,retain)IBOUTlet UILabel *lbl;
@end
ViewController2nd.m
#import "ViewController2nd.h"
@interface ViewController2nd ()
@end
@implementation ViewController2nd
@synthesize lbl;
- (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
答案 2 :(得分:0)
答案 3 :(得分:0)
首先,您必须初始化ViewController2nd
的对象。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
ViewController2nd *objeView2nd = [[ViewController2nd alloc]init];
objeView2nd.lbl.text = [NSString stringWithFormat:@"Hello, %@",txtField.text]
[txtField resignFirstResponder];
return YES;
}
还有一件事,lbl
ViewController2nd
必须是@property
和@synthesize
。没有它,您将无法访问lbl
。
答案 4 :(得分:0)
您始终可以使用Singleton Class来回传递数据。例如,appDelegate类是单例类。您可以使用该类的共享实例来回传递数据。
示例:
Step 1:
yourAppDelegate.h
@property (strong, nonatomic) NSString *txtLabelText;
yourAppDelegate.m
@synthesize txtLabelText;
Step 2:
viewController1.m
#import viewController1.m
#import yourAppDelegate.m
-(void)viewDidLoad{
UILabel *txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,50,100,20)];
txtLabel.text = @"Rebel Yell";
yourAppDelegate *appDelegate = (yourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.txtLabelText = txtLabel.text;
}
Step 3:
viewController2.m
#import viewController2.m
#import yourAppDelegate.m
-(void)viewDidLoad{
yourAppDelegate *appDelegate = (yourAppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(@"Passed UILabel Text from viewController 1 %@", appDelegate.txtLabelText);
}
我建议您创建一个自定义单例类并使用该类的共享实例,而不是仅仅依赖于appDelegate类。
希望这会帮助你澄清一些疑惑