0
votes

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

3
How do you show the second view controller, and where? I'm wondering if you are not creating a whole new ViewController2 object.Larme
I'm using tab bar and a swipe to navigate between these view controllers @LarmeAssam AlZookery
then do not create view controllers on your own as you currently do. @Larme is 100% correct.luk2302
@luk2302 any suggestions how to make it workAssam AlZookery

3 Answers

1
votes

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.

1
votes

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

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

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 *strthen you can access it in any view controller by appDelegate.str