Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pass data from one ViewController to another in iOS

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 623
    Comment on it

    I’ve just created second view controller class by extending from the UIViewController class. However, it doesn’t differ from the parent class until we add our own variables and methods. There are a couple of things we have to change:

    • Assign a variable (textFromFirstView) for data passing – when user taps a button in the first view , there must be a way to pass the text to the second view.
    • Assign a variable (textToShow) for the text label – presently the label is static. It should be updated as the text changes.

    Okay, let’s add these two variables (textToShow and textFromFirstView). Select the “SecondViewController.h” and adds two properties for the interface:

    @interface SecondViewController : UIViewController
    
    @property (strong, nonatomic) IBOutlet UILabel *textToShow;
    @property (strong, nonatomic) NSString *textFromFirstView;
    @end

    Now in the First view controller when a segue gets perform it automatically calls  PrepareForSegue  method. Inside this method create object of second viewcontroller and pass the value like this:

    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    
            SecondViewController *secondVC = [segue destinationViewController];
            secondVC.textFromFirstView = @"This is raw text of First view Controller.";
    
    }

    And finally in the secondviewcontroller set the text string from the property on to the label.

    Thanks

       

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: