2012-10-22 22 views
0

我正在使用多个视图控制器并尝试将一个视图控制器中的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

所以如果你想比你要做到这一点与ViewController2nd的'对象使用它在'ViewController'我想你已经宣布''lbl'in ViewController2nd' '。 – Krunal

回答

1

你不必范围/过剩ViewControllerViewController2ndIBOutlet UILabel *lbl;的为此,你需要自定义的代表,一个ViewControllerViewController2nd的委托,并通过回数据。有关更多详细信息,请参阅this post

+0

我在视图控制器中添加了这个:#import“ViewController2nd.h” – lakesh

+0

@lakesh你可否理解委托是错误的。 –

+0

@lakesh它很好,你已经导入视图,但你必须初始化'ViewController2nd'的对象,并与该对象,你可以使用'lbl'。 – Krunal

0

lbl声明错class.Post的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 
0

错误是正确的

有一个在视图控制器为LBL :)

你必须使用ViewController2nd对象来访问它的实例属性中没有元素

Here is一个开始面向对象的专业版用Objective-C编写。

0

首先你必须初始化对象ViewController2nd

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
ViewController2nd *objeView2nd = [[ViewController2nd alloc]init]; 
objeView2nd.lbl.text = [NSString stringWithFormat:@"Hello, %@",txtField.text] 
[txtField resignFirstResponder]; 
return YES; 
} 

还有一件事,的ViewController2ndlbl必须是@property@synthesize。没有这个,你不能访问lbl

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类中,我会建议你做一个自定义的单例类,并使用类的共享实例。

希望这会帮助你清楚有些疑惑

相关问题