Using Delegates in multiple view controllers

2019-09-17 02:37发布

I am using multiple view controllers and trying the link the UITextfield in one view controller to the label in another view controller using delegate.

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

The error i get is:

Use of undeclared identifier lbl in Viewcontroller.m

Not sure how to solve this problem. Need some guidance.. Thanks...

5条回答
霸刀☆藐视天下
2楼-- · 2019-09-17 02:44

You don't have scope/excess of ViewController2nd's IBOutlet UILabel *lbl; in ViewController for this you need custom delegates, a ViewController a delegate of ViewController2nd and pass data back. Have a look at this post for more details.

查看更多
Rolldiameter
3楼-- · 2019-09-17 02:49

The declaration of lbl is done in the wrong class.Post the IBOutlet UILabel *lbl; within the interface of viewcontroller.h

incase if you want the use of variable from other class change the code to

#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
查看更多
家丑人穷心不美
4楼-- · 2019-09-17 02:52

Error is correct

There is no element in ViewController as lbl :)

You have to use ViewController2nd object to access its instance properties

Here is a place to start for object oriented programming with Objective-C.

查看更多
淡お忘
5楼-- · 2019-09-17 02:55

You can always use Singleton Class to pass data back and forth. For example the appDelegate class is the singleton class. You can use shared instance of that class to pass data back and forth.

Example:

      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);

                     }

Instead of only relying on appDelegate class, I would recommend you to make a custom singleton class and use shared instance of that class.

Hope this would help you clear some doubts

查看更多
淡お忘
6楼-- · 2019-09-17 03:00

First you have to init object of ViewController2nd.

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

And one more thing, lbl of ViewController2nd must have to be @property and @synthesize. Without that, you can't access lbl.

查看更多
登录 后发表回答