Using Delegates in multiple view controllers

大憨熊 提交于 2020-01-16 14:44:51

问题


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...


回答1:


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.




回答2:


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.




回答3:


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:


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.




回答5:


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



来源:https://stackoverflow.com/questions/13008965/using-delegates-in-multiple-view-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!