2012-10-22 21 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類中,我會建議你做一個自定義的單例類,並使用類的共享實例。

希望這會幫助你清楚有些疑惑

相關問題