Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在多个视图控制器中使用代理_Iphone_Ios_Delegates - Fatal编程技术网

Iphone 在多个视图控制器中使用代理

Iphone 在多个视图控制器中使用代理,iphone,ios,delegates,Iphone,Ios,Delegates,我正在使用多个视图控制器,并尝试使用委托将一个视图控制器中的UITextfield链接到另一个视图控制器中的标签 ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> { IBOutlet UITextField *txtField; } @end ViewController2nd.h #imp

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

ViewController.h

#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
的对象执行此操作。