0

我正在使用多个视图控制器并尝试使用委托将一个视图控制器中的 UITextfield 链接到另一个视图控制器中的标签。

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField *txtField;

}

@end

视图控制器.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

不知道如何解决这个问题。需要一些指导..谢谢...

4

5 回答 5

1

为此,您没有ViewController2nd'sIBOutlet UILabel *lbl;的范围/过多,ViewController您需要自定义委托、ViewController一个委托ViewController2nd并传回数据。看看这篇文章了解更多细节。

于 2012-10-22T10:02:22.753 回答
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

视图控制器.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
于 2012-10-22T10:05:46.610 回答
0

错误是正确的

ViewController 中没有元素作为 lbl :)

您必须使用 ViewController2nd 对象来访问其实例属性

这是使用 Objective-C 进行面向对象编程的起点。

于 2012-10-22T10:08:49.543 回答
0

首先你必须初始化ViewController2nd.

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

还有一件事,lblofViewController2nd必须是@propertyand @synthesize。没有它,您将无法访问lbl.

于 2012-10-22T10:17:32.637 回答
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 类。

希望这可以帮助您消除一些疑问

于 2012-10-23T05:21:46.567 回答