iOS Storyboard Passing Data Navigationviewcontroller

How do I instantiate navigationController while passing data to viewController?

That's because the initial UIViewController of that particular storyboard is a UINavigationController.

A simple way to get the VC that you want is getting such UINavigationController by calling:

navController = [storyboard instantiateInitialViewController];

just like you've done, and then:

navController.childViewControllers[0]

This will return the first VC of that particular navigation controller (assuming, of course, that it contains solely the VC that it's embedded in), which is probably your ExperiencesListViewController

How to Pass Data Object to View Controller Embedded in Navigation Controller in Different Storyboard

Not sure without seeing a full example of your code, but I you must be missing something...

Here is a complete example. It should be obvious what gets connected to @IBOutlet and @IBAction (and Storyboard IDs):

UserObject.h

//
// UserObject.h
// Created by Don Mag on 4/1/20.
//

#import <Foundation/Foundation.h>

@interface UserObject : NSObject

@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (assign, readwrite) NSInteger age;

- (NSString *)name;

@end

UserObject.m

//
// UserObject.m
// Created by Don Mag on 4/1/20.
//

#import "UserObject.h"

@implementation UserObject

- (NSString *)name {
return [NSString stringWithFormat:@"%@, %@", _lastName, _firstName];
}

@end

** FirstViewController.h**

//
// FirstViewController.h
// Created by Don Mag on 4/1/20.
//

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
@end

** FirstViewController.m**

//
// FirstViewController.m
// Created by Don Mag on 4/1/20.
//

#import "FirstViewController.h"
#import "UserDetailViewController.h"
#import "UserObject.h"

@interface FirstViewController ()

@property (strong, nonatomic) UserObject *aUserObject;
@property (assign, readwrite) NSInteger iAge;

@end

@implementation FirstViewController

- (void)viewDidLoad {
[super viewDidLoad];

// initialize age
_iAge = 25;

// initialize a new UserObject
_aUserObject = [UserObject new];

_aUserObject.firstName = @"John";
_aUserObject.lastName = @"Smith";
_aUserObject.age = _iAge;

}

- (IBAction)didTap:(id)sender {

UIStoryboard *sb2 = [UIStoryboard storyboardWithName:@"secondSB" bundle:nil];
UINavigationController* nav = [sb2 instantiateViewControllerWithIdentifier:@"userNav"];

UserDetailViewController *destVC = (UserDetailViewController * )nav.topViewController;

// increment age, so it changes each time we call this method
_iAge++;

_aUserObject.age = _iAge;

destVC.userObj = _aUserObject;

[self presentViewController:nav animated:YES completion:nil];

}

@end

UserDetailViewController.h (VC is in second storyboard)

//
// UserDetailViewController.h
// Created by Don Mag on 3/31/20.
//

#import <UIKit/UIKit.h>

#import "UserObject.h"

@interface UserDetailViewController : UIViewController

@property (strong, nonatomic) UserObject *userObj;

@end

UserDetailViewController.m

//
// UserDetailViewController.m
// Created by Don Mag on 3/31/20.
//

#import "UserDetailViewController.h"

@interface UserDetailViewController ()
@property (strong, nonatomic) IBOutlet UILabel *userLabel;
@end

@implementation UserDetailViewController

- (void)viewDidLoad {
[super viewDidLoad];

_userLabel.text = [NSString stringWithFormat:
@"_useObj.firstName: %@ \n" \
"_userObj.lastName: %@ \n" \
"_userObj.age: %ld \n" \
"_userObj name method: %@",
_userObj.firstName,
_userObj.lastName,
_userObj.age,
[_userObj name]];

}

@end

In case it's not completely clear, here is a working example app: https://github.com/DonMag/DataObjectPassing

Passing Data Via NavigationController

You can access your viewController from viewControllers property of UINavigationController.

if (navigationController.viewControllers.count > 0) {
//Cast UIViewController with your custom Controller
ViewController *vc = (ViewController*) [navigationController.viewControllers firstObject];
//Now pass data you want
vc.passData = ...
}

iOS storyboard passing data navigationViewController

UINavigationController has a property called topViewController which returns the view controller that is at the top of the stack.

So your prepareForSegue: method may look something like this...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"sLogowanieFirmy"]) {
UINavigationController *nav = [segue destinationViewController];
FirmyVC *firmyVC = (FirmyVC *)nav.topViewController;
firmyVC.tabFirmy = self.tabFirmy;
}

// etc...
}

How to pass data from view controller to a navigation controller and then to another view controller?

You can use this in First ViewController:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentPopup"
{
let destViewController = segue.destination as! NavigationViewController
let secondViewcontroller = destViewController.viewcontrollers.first as! SecondViewcontroller
secondViewcontroller.myData2 = myData
}
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}

Swift: pass data to first child of NavigationController instantiate with storyboardID

After instantiating the navigation controller from the storyboard, you will be able to access the root view controller via navigationController.viewControllers.first.

guard let navigationController = storyboard?.instantiateViewController(withIdentifier: identifier) as? UINavigationController else { return }

if let chatBotViewController = navigationController.viewControllers.first as? ChatBotViewController {
chatBotViewController.fittoBottle = fittoBottle
}

self.dismiss(animated: false, completion: nil)
self.present(navigationController, animated: true, completion: nil)

Passing data with segue through navigationController

The destination view controller of the segue is the UINavigationController. You need to ask it for its top view controller to get to the real destination view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showItemSegue" {
let navController = segue.destination as! UINavigationController
let detailController = navController.topViewController as! ShowItemViewController
detailController.currentId = nextId!
}
}

Passing Data between view Controllers Using a segue from a view embedded in a navigation controller to a tabbarcontroller

This is my view controller where you can check that I am sending 5 to tabbar first viewcontroller:

   class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.performSegue(withIdentifier: "segueIdentifier", sender: self)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationNv = barViewControllers.viewControllers?[0] as! UINavigationController
let destinationViewController = destinationNv.viewControllers[0] as! FirstViewController
destinationViewController.currentBalance = 5
}
}

Now You can check my firstview controller where you can check that what value we are getting.

class FirstViewController: UIViewController {

var currentBalance = 0
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
print(currentBalance)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Now, You can check my console and storyboard:
Sample Image

Sample Image

Pass data before popViewController without segue and storyboard

Use these links to understand exactly how to use Protocols in swift:

  • Passing data between two ViewControllers (delegate) - Swift

  • Passing Data between View Controllers

You have to implement below line of code in first view controller :-

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
let secondViewController = segue.destination as! SecondViewController
secondViewController.delegate = self
}
}


Related Topics



Leave a reply



Submit