Iphone 在多个视图控制器中使用代理
我正在使用多个视图控制器,并尝试使用委托将一个视图控制器中的UITextfield链接到另一个视图控制器中的标签 ViewController.hIphone 在多个视图控制器中使用代理,iphone,ios,delegates,Iphone,Ios,Delegates,我正在使用多个视图控制器,并尝试使用委托将一个视图控制器中的UITextfield链接到另一个视图控制器中的标签 ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> { IBOutlet UITextField *txtField; } @end ViewController2nd.h #imp
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *txtField;
}
@end
ViewController2nd.h
#import <UIKit/UIKit.h>
@interface ViewController2nd : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *lbl;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController2nd : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *lbl;
}
@property(nonatomic,retain)IBOUTlet UILabel *lbl;
@end
我得到的错误是:
在Viewcontroller.m中使用未声明的标识符lbl
#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
#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
的IBOutlet UILabel*lbl的作用域/超出范围
在ViewController
中,为此,您需要自定义委托,一个ViewController
的委托,一个ViewController2nd
的委托,并传回数据。查看更多详细信息。lbl的声明在错误的类别中完成。发布iblabel*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
ViewController2nd.h
#import <UIKit/UIKit.h>
@interface ViewController2nd : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *lbl;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController2nd : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *lbl;
}
@property(nonatomic,retain)IBOUTlet UILabel *lbl;
@end
错误是正确的
ViewController中没有lbl形式的元素:)
必须使用ViewController2nd对象才能访问其实例属性
使用Objective-C进行面向对象编程的起点。首先必须初始化ViewController2nd的对象
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
ViewController2nd *objeView2nd = [[ViewController2nd alloc]init];
objeView2nd.lbl.text = [NSString stringWithFormat:@"Hello, %@",txtField.text]
[txtField resignFirstResponder];
return YES;
}
还有一件事,ViewController2nd
的lbl
必须是@property
和@synthesis
。否则,您将无法访问lbl
您可以始终使用Singleton类来来回传递数据。例如,appDelegate类是singleton类。您可以使用该类的共享实例来来回传递数据
例如:
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类
希望这能帮助您消除一些疑问我在中添加了以下内容:#导入“ViewController2nd.h”viewcontroller@lakesh请您理解委托是错误的。@lakesh导入视图很好,但您必须初始化ViewController2nd
的对象,使用该对象,您可以使用lbl
。我该怎么做?意思是说我必须输入iblabel*lbl;又在viewcontroller.m?@lakesh:buddy,你没有告诉我更多的细节,你想做什么,你的应用程序的流程是什么?是先显示ViewCont2,然后再显示VC1还是先显示VC1?我想您已经在ViewController2nd
中声明了lbl
,因此如果您想在ViewController
中使用它,您必须对ViewController2nd
的对象执行此操作。