简体   繁体   中英

Second view controller has no data

I have the delegate method I implement to pass data from FirstViewController to SecondViewController .

First, I created a text Field and Button in FirstViewController. Second, I created a label to display the data in SecondViewController.

How it should work: The user will press the button in FirstViewController and it will pass the data to the secondViewController .In addition,the user will be using a UISwipeGestureRecognizer to navigate to the second view controller, so there's no need to use the button to go to the second view controller.

How I test the application

1- I run the application . --> 2- I type "Hello" in Text field --> 3- I press the button --> 4- I swipe to the second view controller to see the data --> 5- I don't see any data in the label ??

First i created the delegate protocol

FirstViewController.h

@class FirstViewController;

@protocol FirstControllerDelegate

-(void) updateLabel:(NSString*)string

@end

@property (weak, nonatomic) id<FirstViewControllerDelegate>delegate;
@property (weak, nonatomic) IBOutlet UITextField *TextField;
- (IBAction)button:(id)sender;

FirstViewController.m

@interface FirstViewController. ()
@end
@implementation ViewController1
@synthesize delegate;
@synthesize TextField;

-(void)viewDidLoad{
    [super viewDidLoad];
}
- (IBAction)Button:(id)sender {

  ViewController2 *vc2 = [[ViewController2 alloc] init];
   vc2.stringfromTextfield1 = self.TextField.text;

}

SecondViewController.h

#import "SecondViewController.h"
#import "FirstViewController.h"
@interface SecondViewController : UITableViewController<FirstControllerDelegate> 
@end
@property (weak, nonatomic) IBOutlet UILabel *Label;
@property (strong, nonatomic) NSString *stringfromTextfield1; 

SecondViewController.m

@interface SecondViewController. ()
    @end
    @implementation SecondViewController

    -(void)viewDidLoad{
        [super viewDidLoad];

     self.label.text = self.stringfromTextfield1;

    }

I appreciate your time and effort to help me out

You can use Singleton class for this, or you can use extern NSString for move the string value one to second View Controller.

in first view controller.

extern NSString *MyStr;

in the @interface define it.

@interface{
NSString *MyStr;
}

In the @Implementation assign the value what you want.

and the second view controller just #import the first view and use,

MyStr As a variable and its having the value also from first view controller. This thing worked for me.

Or simply add an AppDelegate property to store the variable in both ViewControllers:

AppDelegate *appDel=[[UIApplication sharedApplication] delegate];
appDel.variable=@"text";

You can save data in App delegate and access it across view controllers in your application.You have to create a shared instance of app delegate.

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

like this

Suppose you declare a NSString object *str then you can access it in any view controller by appDelegate.str

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM