I don't want to reload view controller - objective-c

I'm using storyboard & segue to go from "HomeVC" to "MapVC".
HomeVC - > segue -> MapVC
And since navigation controller is embedded, MapVC automatically get back button.
Now when I'm again coming from HomeVC->MapVC, MapVC is getting reloaded. Which I don't want. Is there any way to avoid doing it programmatically.

I'd not use a segue but an IBAction and do the push manually:
e.g.
hook up a UIButton (or whatever) to trigger #IBAction func buttonClicked(sender: UIButton!)
keep a reference to mapVC as an outlet: #IBOutlet var mapVC:MyMapViewController!
[note: you could also initialize the VC manually]
implement the action to push the same mapVC
#IBAction func buttonClicked(sender: UIButton!) {
navigationController.pushViewController(mapVC, animated:true)
}
IMHO easiest / pragmatic
alternative idea: keep the segue, but connect it to a blank VC. in prepareForSegue add mapVC as a child of the segue's destinationViewController
IMO this is bad
best way I think: make the mapVC correctly persist and reload it's view based on some state you save in the homeVC.. e.g. keep visibleRegion
IMHO this is best and is how VC's are meant to be used

When you push MapVC from HomeVC, a new object of MapVC is created each time. Its not that MapVC is reloaded, instead a new instance is created each time.

If you're using Storyboard Segues, a new instance of your MapVC will be created every time the Segue is executed. That's just how a Segue works. To solve it you could keep a strong reference to your MapVC in the HomeVC and push that instead. The steps involved are to remove the Segue in your storyboard and assign a Storyboard Identifier on your MapVC. You probably want to create an #IBAction to added to a button to trigger the push. Let's say you set "idMapVC" as the identifier for your MapVC in your storyboard. You'd end up with code like this:
class HomeVC: UIViewController {
var mapVC: MapVC?
override func viewDidLoad() {
super.viewDidLoad()
mapVC = storyboard?.instantiateViewController(withIdentifier: "idMapVC") as? MapVC
}
#IBAction func pushTheMap() {
guard let mapVC = self.mapVC
else { return }
navigationController?.pushViewController(mapVC, animated: true)
}
}

What do you actually want to achieve? Do you not want to display the view of MapVC?
If you stored some state in the MapVC that you are losing, then you are doing something very, very wrong. VC stands for View Controller. It controls the view. It must never, ever contain your data. So deleting one view controller and creating a new one should be absolutely painless.

ViewController always loaded in stack first in last out so when you connect any viewController using push segue firstly it loads the whole viewController inside the memory and when you go back to previously loaded view it will not load again so the process goes so on.
Here is atrick if you opt. You need to do just set the NSUserDefault key true using dispatch_once GCD block inside MapVC viewDidLoad method accordingly and check before it loads.
Example:
// MapVC viewController
- (void)viewDidLoad {
[super viewDidLoad];
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
// set the key true here
});
if([[[NSUserDefaults standardUserDefaults] valueForKey:#"yourKey"] isEqualToString:#"true"]){
// code here which you do not wish to execute again and again
// Note: set the key false here so this block will not execute again
}
}
Note: set the key true again somewhere else if you want to execute that particular code again.

Related

Object becomes Nil when pushing controller

I have a the first application controller, MAViewControllerMenu, and when that controller loads, I already allocate the next controller, imageControllerView.
- (void)viewDidAppear{
[super viewDidAppear:(YES)];
if (!imageControllerView)
imageControllerView = [[self storyboard] instantiateViewControllerWithIdentifier:#"chosenImageController"];
}
Then, I select an image from the image picker, and want to move to the next controller,imageControllerView, where the image would be displayed. I set the next window's image property as follows:
imageControllerView.image = [[self.pageViews objectAtIndex:(centered_image_ind)] image];
This line works, I checked that there's a value in imageControllerView.image.
However, when I move to the next controller,imageControllerView , I notice that the memory address of imageControllerView changes, or in other words, imageControllerView's properties that I change before moving to that controller, specifically image, reset when I move there.
Instead of throwing code here, I was hoping you could let me know what I should provide.
I think it's a common problem people know of:
Controller's objects re-init'ing when moving from one controller to another.
Here's a screen shot that might give a hint of what Im trying to do
Left most one is where I select pictures which in turn go into the slide show scrollview. Then I click next, and the image is supposed to appear in the centered ImageView
Thanks
OK...
You cannot "already allocate the next view controller" this won't work. There is no point in creating it like this at all. You can delete the imageViewController property (or iVar) completely.
The arrows that you have between the view controllers in your storyboard are segues. In Interface Builder you can select a segue and give it an identifier. For instance you would use something like #"ImageViewSegue".
I guess the segue is attached to the Next button. This is fine.
Now, in your MAViewControllerMenu you need to put this method...
- (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ImageViewSegue"]) {
// the controller is CREATED by the segue.
// the one you create in view did load is never used
ImageViewController *controller = segue.destinationController;
controller.image = [[self.pageViews objectAtIndex:(centered_image_ind)] image];
}
}
Now for the segues in the other direction...
You appear to be using segues to dismiss the modal views. You can't do this. What it will do is create a new view controller and present that instead of dismissing the presented view.
i.e. you'll go...
A -> B -> C -> B -> A -> B
// you'll now have 6 view controllers in memory
// each segue will create a fresh view controller with no values set.
What you want is...
A -> B -> C
A -> B
A
// now you only have one view controller because the others were dismissed.
// when you dismiss a view controller it will go back to the original one.
// the original one will have all the values you set previously.
To do this you need to create a method something like...
- (IBAction)dismissView
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Then whatever the button is for your dismiss action attach it to this method.
Now delete all the backwards curly segues.
Passing info back
To pass info back to the original view controller you need a delegation pattern or something similar.
You can read more about creating a delegate at This random Google Search
Create a delegate method something like...
- (void)imageViewSelectedImage:(UIImage *)image;
or something like this.
Now when you do prepareForSegue you can do...
controller.delegate = self;
and have a method...
- (void)imageViewSelectedImage:(UIImage *)image
{
// save the method that has been sent back into an array or something
}
I might be wrong, but seems you go to your second view controller using a segue, it is normal your controller instance isn't the same than the one retrieved by [[self storyboard] instantiateViewControllerWithIdentifier:#"chosenImageController"]
you should take a look at - (void) prepareForSegue:(UIStoryboardSegue *)segue
(UIViewController method)
inside this method set your image property to the segue destination controller (check the identifier of the segue)

how to add textfield data into another page in tableview cell [duplicate]

I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following:
I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with a UITableViewController and I have enabled multiple selections.
How do I transfer the data from one view to another? I will be holding the selections on the UITableView in an array, but how do I then pass that back to the previous data entry form view so it can be saved along with the other data to Core Data on submission of the form?
I have surfed around and seen some people declare an array in the app delegate. I read something about singletons, but I don't understand what these are and I read something about creating a data model.
What would be the correct way of performing this and how would I go about it?
This question seems to be very popular here on Stack Overflow so I thought I would try and give a better answer to help out people starting in the world of iOS like me.
Passing Data Forward
Passing data forward to a view controller from another view controller. You would use this method if you wanted to pass an object/value from one view controller to another view controller that you may be pushing on to a navigation stack.
For this example, we will have ViewControllerA and ViewControllerB
To pass a BOOL value from ViewControllerA to ViewControllerB we would do the following.
in ViewControllerB.h create a property for the BOOL
#property (nonatomic, assign) BOOL isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB so use an
#import "ViewControllerB.h"
Then where you want to load the view, for example, didSelectRowAtIndex or some IBAction, you need to set the property in ViewControllerB before you push it onto the navigation stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:#"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
Passing Data Forward using Segues
If you are using Storyboards you are most likely using segues and will need this procedure to pass data forward. This is similar to the above but instead of passing the data before you push the view controller, you use a method called
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
So to pass a BOOL from ViewControllerA to ViewControllerB we would do the following:
in ViewControllerB.h create a property for the BOOL
#property (nonatomic, assign) BOOL isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB, so use an
#import "ViewControllerB.h"
Create the segue from ViewControllerA to ViewControllerB on the storyboard and give it an identifier. In this example we'll call it "showDetailSegue"
Next, we need to add the method to ViewControllerA that is called when any segue is performed. Because of this we need to detect which segue was called and then do something. In our example, we will check for "showDetailSegue" and if that's performed, we will pass our BOOL value to ViewControllerB
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showDetailSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;
}
}
If you have your views embedded in a navigation controller, you need to change the method above slightly to the following
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showDetailSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
controller.isSomethingEnabled = YES;
}
}
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
Passing Data Back
To pass data back from ViewControllerB to ViewControllerA you need to use Protocols and Delegates or Blocks, the latter can be used as a loosely coupled mechanism for callbacks.
To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
For ViewControllerA to be a delegate of ViewControllerB it must conform to ViewControllerB's protocol which we have to specify. This tells ViewControllerA which methods it must implement.
In ViewControllerB.h, below the #import, but above #interface you specify the protocol.
#class ViewControllerB;
#protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
#end
Next still in the ViewControllerB.h, you need to set up a delegate property and synthesize in ViewControllerB.m
#property (nonatomic, weak) id <ViewControllerBDelegate> delegate;
In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack = #"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
#interface ViewControllerA : UIViewController <ViewControllerBDelegate>
In ViewControllerA.m implement the following method from our protocol
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
NSLog(#"This was returned from ViewControllerB %#", item);
}
Before pushing viewControllerB to navigation stack we need to tell ViewControllerB that ViewControllerA is its delegate, otherwise we will get an error.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:#"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];
References
Using Delegation to Communicate With Other View Controllers in the View Controller Programming Guide
Delegate Pattern
NSNotification center
It's another way to pass data.
// Add an observer in controller(s) where you want to receive data
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleDeepLinking:) name:#"handleDeepLinking" object:nil];
-(void) handleDeepLinking:(NSNotification *) notification {
id someObject = notification.object // Some custom object that was passed with notification fire.
}
// Post notification
id someObject;
[NSNotificationCenter.defaultCenter postNotificationName:#"handleDeepLinking" object:someObject];
Passing Data back from one class to another (A class can be any controller, Network/session manager, UIView subclass or any other class)
Blocks are anonymous functions.
This example passes data from Controller B to Controller A
Define a block
#property void(^selectedVoucherBlock)(NSString *); // in ContollerA.h
Add block handler (listener)
Where you need a value (for example, you need your API response in ControllerA or you need ContorllerB data on A)
// In ContollerA.m
- (void)viewDidLoad {
[super viewDidLoad];
__unsafe_unretained typeof(self) weakSelf = self;
self.selectedVoucherBlock = ^(NSString *voucher) {
weakSelf->someLabel.text = voucher;
};
}
Go to Controller B
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ControllerB *vc = [storyboard instantiateViewControllerWithIdentifier:#"ControllerB"];
vc.sourceVC = self;
[self.navigationController pushViewController:vc animated:NO];
Fire block
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSString *voucher = vouchersArray[indexPath.row];
if (sourceVC.selectVoucherBlock) {
sourceVC.selectVoucherBlock(voucher);
}
[self.navigationController popToViewController:sourceVC animated:YES];
}
Another Working Example for Blocks
Swift
There are tons and tons of explanations here and around Stack Overflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it).
YouTube tutorial: How to send data through segue (Swift)
Passing data forward to the next View Controller
The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
Create the storyboard layout in the Interface Builder. To make the segue, you just Control click on the button and drag over to the Second View Controller.
First View Controller
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get a reference to the second view controller
let secondViewController = segue.destination as! SecondViewController
// Set a variable in the second view controller with the String to pass
secondViewController.receivedString = textField.text!
}
}
Second View Controller
And the code for the Second View Controller is
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
// This variable will hold the data being passed from the First View Controller
var receivedString = ""
override func viewDidLoad() {
super.viewDidLoad()
// Used the text from the First View Controller to set the label
label.text = receivedString
}
}
Don't forget
Hook up the outlets for the UITextField and the UILabel.
Set the first and second View Controllers to the appropriate Swift files in Interface Builder.
Passing data back to the previous View Controller
To pass data back from the second view controller to the first view controller, you use a protocol and a delegate. This video is a very clear walk though of that process:
YouTube tutorial: iOS Swift Basics Tutorial: Protocols and Delegates But also read this post to make sure you don't get into a strong reference cycle.
The following is an example based on the video (with a few modifications).
Create the storyboard layout in the Interface Builder. Again, to make the segue, you just Control drag from the button to the Second View Controller. Set the segue identifier to showSecondViewController. Also, don't forget to hook up the outlets and actions using the names in the following code.
First View Controller
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController, DataEnteredDelegate {
#IBOutlet weak var label: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
let secondViewController = segue.destination as! SecondViewController
secondViewController.delegate = self
}
}
func userDidEnterInformation(info: String) {
label.text = info
}
}
Note the use of our custom DataEnteredDelegate protocol.
Second View Controller and Protocol
The code for the second view controller is
import UIKit
// Protocol used for sending data back
protocol DataEnteredDelegate: AnyObject {
func userDidEnterInformation(info: String)
}
class SecondViewController: UIViewController {
// Making this a weak variable, so that it won't create a strong reference cycle
weak var delegate: DataEnteredDelegate? = nil
#IBOutlet weak var textField: UITextField!
#IBAction func sendTextBackButton(sender: AnyObject) {
// Call this method on whichever class implements our delegate protocol
delegate?.userDidEnterInformation(info: textField.text!)
// Go back to the previous view controller
_ = self.navigationController?.popViewController(animated: true)
}
}
Note that the protocol is outside of the View Controller class.
That's it. Running the app now, you should be able to send data back from the second view controller to the first.
The M in MVC is for "Model" and in the MVC paradigm the role of model classes is to manage a program's data. A model is the opposite of a view -- a view knows how to display data, but it knows nothing about what to do with data, whereas a model knows everything about how to work with data, but nothing about how to display it. Models can be complicated, but they don't have to be -- the model for your app might be as simple as an array of strings or dictionaries.
The role of a controller is to mediate between view and model. Therefore, they need a reference to one or more view objects and one or more model objects. Let's say that your model is an array of dictionaries, with each dictionary representing one row in your table. The root view for your app displays that table, and it might be responsible for loading the array from a file. When the user decides to add a new row to the table, they tap some button and your controller creates a new (mutable) dictionary and adds it to the array. In order to fill in the row, the controller creates a detail view controller and gives it the new dictionary. The detail view controller fills in the dictionary and returns. The dictionary is already part of the model, so nothing else needs to happen.
There are various ways by which data can be received by a different class in iOS. For example -
Direct initialization after the allocation of another class.
Delegation - for passing data back
Notification - for broadcasting data to multiple classes at a single time
Saving in NSUserDefaults - for accessing it later
Singleton classes
Databases and other storage mechanisms, like p-list files, etc.
But for the simple scenario of passing a value to a different class whose allocation is done in the current class, the most common and preferred method would be the direct setting of values after allocation. This is done as follows:
We can understand it using two controllers - Controller1 and Controller2
Suppose in Controller1 class you want to create the Controller2 object and push it with a String value being passed. This can be done as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passValue:#"String"];
[self pushViewController:obj animated:YES];
}
In the implementation of the Controller2 class there will be this function as:
#interface Controller2 : NSObject
#property (nonatomic, strong) NSString* stringPassed;
#end
#implementation Controller2
#synthesize stringPassed = _stringPassed;
- (void) passValue:(NSString *)value {
_stringPassed = value; // Or self.stringPassed = value
}
#end
You can also directly set the properties of the Controller2 class in the similar way as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj setStringPassed:#"String"];
[self pushViewController:obj animated:YES];
}
To pass multiple values, you can use the multiple parameters like:
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passValue:#“String1” andValues:objArray withDate:date];
Or if you need to pass more than three parameters which are related to a common feature, you can store the values in a model class and pass that modelObject to the next class
ModelClass *modelObject = [[ModelClass alloc] init];
modelObject.property1 = _property1;
modelObject.property2 = _property2;
modelObject.property3 = _property3;
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passmodel: modelObject];
So in short, if you want to -
set the private variables of the second class initialise the values by calling a custom function and passing the values.
setProperties do it by directlyInitialising it using the setter method.
pass more that 3-4 values related to each other in some manner, then create a model class and set values to its object and pass the object using any of the above process.
After more research it seemed that protocols and delegates were the correct/Apple preferred way of doing this.
I ended up using this example (in the iPhone development SDK):
Sharing data between view controllers and other objects
It worked fine and allowed me to pass a string and an array forward and back between my views.
I find simplest and most elegant version with passing blocks.
Let's name view controller that waits for returned data as "A" and returning view controller as "B". In this example we want to get 2 values: first of Type1 and second of Type2.
Assuming we use Storyboard, first controller sets callback block, for example during segue preparation:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[BViewController class]])
{
BViewController *viewController = segue.destinationViewController;
viewController.callback = ^(Type1 *value1, Type2 *value2) {
// optionally, close B
//[self.navigationController popViewControllerAnimated:YES];
// let's do some action after with returned values
action1(value1);
action2(value2);
};
}
}
and "B" view controller should declare callback property, BViewController.h:
// it is important to use "copy"
#property (copy) void(^callback)(Type1 *value1, Type2 *value2);
Than in implementation file BViewController.m after we have desired values to return our callback should be called:
if (self.callback)
self.callback(value1, value2);
One thing to remember is that using block often needs to manage strong and __weak references like explained here
There is some good information in many of the answers given, but none address the question fully.
The question asks about passing information between view controllers. The specific example given asks about passing information between views, but given the self-stated newness to iOS, the original poster likely meant between viewControllers, not between views (without any involvement from the ViewControllers). It seems that all the answers focus on two view controllers, but what if the app evolves to need to involve more than two view controllers in the information exchange?
The original poster also asked about Singletons and the use of the AppDelegate. These questions need to be answered.
To help anyone else looking at this question, who wants a full answer, I'm going to attempt to provide it.
Application Scenarios
Rather than having a highly hypothetical, abstract discussion, it helps to have concrete applications in mind. To help define a two-view-controller situation and a more-than-two-view-controller situation, I am going to define two concrete application scenarios.
Scenario one: maximum two view controllers ever need to share information.
See diagram one.
There are two view controllers in the application. There is a ViewControllerA (Data Entry Form), and View Controller B (Product List). The items selected in the product list must match the items displayed in the text box in the data entry form. In this scenario, ViewControllerA and ViewControllerB must communicate directly with each other and no other view controllers.
Scenario two: more than two view controllers need to share the same information.
See diagram two.
There are four view controllers in the application. It is a tab-based application for managing home inventory. Three view controllers present differently filtered views of the same data:
ViewControllerA - Luxury Items
ViewControllerB - Non-insured Items
ViewControllerC - Entire Home Inventory
ViewControllerD - Add New Item Form
Any time an individual item is created or edited, it must also synchronize with the other view controllers. For example, if we add a boat in ViewControllerD, but it is not yet insured, then the boat must appear when the user goes to ViewControllerA (Luxury Items), and also ViewControllerC (Entire Home Inventory), but not when the user goes to ViewControllerB (Non-insured Items). We need be concerned with not only adding new items, but also deleting items (which may be allowed from any of the four view controllers), or editing existing items (which may be allowed from the "Add New Item Form", repurposing the same for editing).
Since all the view controllers do need to share the same data, all four view controllers need to remain in synchronization, and therefore there needs to be some sort of communication to all other view controllers, whenever any single view controller changes the underlying data. It should be fairly obvious that we do not want each view controller communicating directly with each other view controller in this scenario. In case it is not obvious, consider if we had 20 different view controllers (rather than just 4). How difficult and error-prone would it be to notify each of the other 19 view controllers any time one view controller made a change?
The Solutions: Delegates and the Observer Pattern, and Singletons
In scenario one, we have several viable solutions, as other answers have given
segues
delegates
setting properties on view controllers directly
NSUserDefaults (actually a poor choice)
In scenario two, we have other viable solutions:
Observer Pattern
Singletons
A singleton is an instance of a class, that instance being the only instance in existence during its lifetime. A singleton gets its name from the fact that it is the single instance. Normally developers who use singletons have special class methods for accessing them.
+ (HouseholdInventoryManager*) sharedManager; {
static dispatch_once_t onceQueue;
static HouseholdInventoryManager* _sharedInstance;
// dispatch_once is guaranteed to only be executed
// once in the lifetime of the application
dispatch_once(&onceQueue, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
Now that we understand what a singleton is, let's discuss how a singleton fits into the observer pattern. The observer pattern is used for one object to respond to changes by another object. In the second scenario, we have four different view controllers, who all want to know about changes to the underlying data. The "underlying data" should belong to a single instance, a singleton. The "know about changes" is accomplished by observing changes made to the singleton.
The home inventory application would have a single instance of a class which is designed to manage a list of inventory items. The manager would manage a collection of household items. The following is a class definition for the data manager:
#import <Foundation/Foundation.h>
#class JGCHouseholdInventoryItem;
#interface HouseholdInventoryManager : NSObject
/*!
The global singleton for accessing application data
*/
+ (HouseholdInventoryManager*) sharedManager;
- (NSArray *) entireHouseholdInventory;
- (NSArray *) luxuryItems;
- (NSArray *) nonInsuredItems;
- (void) addHouseholdItemToHomeInventory:(JGCHouseholdInventoryItem*)item;
- (void) editHouseholdItemInHomeInventory:(JGCHouseholdInventoryItem*)item;
- (void) deleteHoueholdItemFromHomeInventory:(JGCHouseholdInventoryItem*)item;
#end
When the collection of home inventory items changes, the view controllers need to be made aware of this change. The class definition above does not make it obvious how this will happen. We need to follow the observer pattern. The view controllers must formally observe the sharedManager. There are two ways to observe another object:
Key-Value-Observing (KVO)
NSNotificationCenter.
In scenario two, we do not have a single property of the HouseholdInventoryManager which could be observed using KVO. Because we do not have a single property which is easily observable, the observer pattern, in this case, must be implemented using NSNotificationCenter. Each of the four view controllers would subscribe to notifications, and the sharedManager would send notifications to the notification center when appropriate. The inventory manager does not need to know anything about the view controllers or instances of any other classes which may be interested in knowing when the collection of inventory items changes; the NSNotificationCenter takes care of these implementation details. The View Controllers simply subscribe to notifications, and the data manager simply posts notifications.
Many beginner programmers take advantage of the fact that there is always exactly one Application Delegate in the lifetime of the application, which is globally accessible. Beginning programmers use this fact to stuff objects and functionality into the appDelegate as a convenience for access from anywhere else in the application. Just because the AppDelegate is a singleton doesn't mean it should replace all other singletons. This is a poor practice as it places too much burden on one class, breaking good object-oriented practices. Each class should have a clear role that is easily explained, often just by the name of the class.
Any time your Application Delegate starts to get bloated, start to remove functionality into singletons. For example, the Core Data Stack should not be left in the AppDelegate, but should instead be put in its own class, a coreDataManager class.
References
Managing Data Flow Between View Controllers
Passing Data Between View Controllers
Asynchronous JSON Requests in Objective-C
Passing data back from ViewController 2 (destination) to viewController 1 (source) is the more interesting thing.
Assuming you use storyBoard, these are all the ways I found out:
Delegate
Notification
User defaults
Singleton
Those were discussed here already.
I found there are more ways:
Using Block callbacks:
Use it in the prepareForSegue method in the VC1
NextViewController *destinationVC = (NextViewController *) segue.destinationViewController;
[destinationVC setDidFinishUsingBlockCallback:^(NextViewController *destinationVC)
{
self.blockLabel.text = destination.blockTextField.text;
}];
Using storyboards Unwind (Exit)
Implement a method with a UIStoryboardSegue argument in VC 1,like this one:
-(IBAction)UnWindDone:(UIStoryboardSegue *)segue { }
In the storyBoard, hook the "return" button to the green Exit button (Unwind) of the vc. Now you have a segue that "goes back" so you can use the destinationViewController property in the prepareForSegue of VC2 and
change any property of VC1 before it goes back.
Another option of using storyboards Undwind (Exit) - you can use the method you wrote in VC1
-(IBAction)UnWindDone:(UIStoryboardSegue *)segue {
NextViewController *nextViewController = segue.sourceViewController;
self.unwindLabel.text = nextViewController.unwindPropertyPass;
}
And in the prepareForSegue of VC1 you can change any property you want to share.
In both unwind options, you can set the tag property of the button and check it in the prepareForSegue.
The OP didn't mention view controllers but so many of the answers do, that I wanted to chime in with what some of the new features of the LLVM allow to make this easier when wanting to pass data from one view controller to another and then getting some results back.
Storyboard segues, ARC and LLVM blocks make this easier than ever for me. Some answers above mentioned storyboards and segues already but still relied on delegation. Defining delegates certainly works but some people may find it easier to pass pointers or code blocks.
With UINavigators and segues, there are easy ways of passing information to the subservient controller and getting the information back. ARC makes passing pointers to things derived from NSObjects simple so if you want the subservient controller to add/change/modify some data for you, pass it a pointer to a mutable instance. Blocks make passing actions easy so if you want the subservient controller to invoke an action on your higher level controller, pass it a block. You define the block to accept any number of arguments that makes sense to you. You can also design the API to use multiple blocks if that suits things better.
Here are two trivial examples of the segue glue. The first is straightforward showing one parameter passed for input, the second for output.
// Prepare the destination view controller by passing it the input we want it to work on
// and the results we will look at when the user has navigated back to this controller's view.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[[segue destinationViewController]
// This parameter gives the next controller the data it works on.
segueHandoffWithInput:self.dataForNextController
// This parameter allows the next controller to pass back results
// by virtue of both controllers having a pointer to the same object.
andResults:self.resultsFromNextController];
}
This second example shows passing a callback block for the second argument. I like using blocks because it keeps the relevant details close together in the source - the higher level source.
// Prepare the destination view controller by passing it the input we want it to work on
// and the callback when it has done its work.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[[segue destinationViewController]
// This parameter gives the next controller the data it works on.
segueHandoffWithInput:self.dataForNextController
// This parameter allows the next controller to pass back results.
resultsBlock:^(id results) {
// This callback could be as involved as you like.
// It can use Grand Central Dispatch to have work done on another thread for example.
[self setResultsFromNextController:results];
}];
}
There are multiple methods for sharing data.
You can always share data using NSUserDefaults. Set the value you want to share with respect to a key of your choice and get the value from NSUserDefault associated to that key in the next view controller.
[[NSUserDefaults standardUserDefaults] setValue:value forKey:key]
[[NSUserDefaults standardUserDefaults] objectForKey:key]
You can just create a property in viewcontrollerA. Create an object of viewcontrollerA in viewcontrollerB and assign the desired value to that property.
You can also create custom delegates for this.
Swift 5
Well Matt Price's answer is perfectly fine for passing data, but I
am going to rewrite it, in the latest Swift version because I believe new
programmers find it quit challenging due to new syntax and
methods/frameworks, as original post is in Objective-C.
There are multiple options for passing data between view controllers.
Using Navigation Controller Push
Using Segue
Using Delegate
Using Notification Observer
Using Block
I am going to rewrite his logic in Swift with the latest iOS framework
Passing Data through Navigation Controller Push: From ViewControllerA to ViewControllerB
Step 1. Declare variable in ViewControllerB
var isSomethingEnabled = false
Step 2. Print Variable in ViewControllerB' ViewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through segue, navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
Step 3. In ViewControllerA Pass Data while pushing through Navigation Controller
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: Passing data through navigation PushViewController
#IBAction func goToViewControllerB(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
// MARK: - Variable for Passing Data through Navigation push
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
}
Passing Data through Segue: From ViewControllerA to ViewControllerB
Step 1. Create Segue from ViewControllerA to ViewControllerB and give Identifier = showDetailSegue in Storyboard as shown below
Step 2. In ViewControllerB Declare a viable named isSomethingEnabled and print its value.
Step 3. In ViewControllerA pass isSomethingEnabled's value while passing Segue
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - - Passing Data through Segue - -
#IBAction func goToViewControllerBUsingSegue(_ sender: Any) {
performSegue(withIdentifier: "showDetailSegue", sender: nil)
}
// Segue Delegate Method
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true//passing data
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through segue
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
}
Passing Data through Delegate: From ViewControllerB to ViewControllerA
Step 1. Declare Protocol ViewControllerBDelegate in the ViewControllerB file, but outside the class
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
Step 2. Declare Delegate variable instance in ViewControllerB
var delegate: ViewControllerBDelegate?
Step 3. Send data for delegate inside viewDidLoad method of ViewControllerB
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
Step 4. Confirm ViewControllerBDelegate in ViewControllerA
class ViewControllerA: UIViewController, ViewControllerBDelegate {
// to do
}
Step 5. Confirm that you will implement a delegate in ViewControllerA
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self//confirming delegate
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
Step 6. Implement delegate method for receiving data in ViewControllerA
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController, ViewControllerBDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
// Delegate method
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
#IBAction func goToViewControllerForDelegate(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
//Protocol decleare
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
class ViewControllerB: UIViewController {
var delegate: ViewControllerBDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - - - - Set Data for Passing Data through Delegate - - - - - -
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
}
}
Passing Data through Notification Observer: From ViewControllerB to ViewControllerA
Step 1. Set and post data in the notification observer in ViewControllerB
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
Step 2. Add Notification Observer in ViewControllerA
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
Step 3. Receive Notification data value in ViewControllerA
#objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification: ", notification.object ?? "")
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Add observer in controller(s) where you want to receive data
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
}
// MARK: Method for receiving Data through Post Notification
#objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification: ", notification.object ?? "")
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// MARK:Set data for Passing Data through Post Notification
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
}
}
Passing Data through Block: From ViewControllerB to ViewControllerA
Step 1. Declare block in ViewControllerB
var authorizationCompletionBlock:((Bool)->())? = {_ in}
Step 2. Set data in block in ViewControllerB
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
Step 3. Receive block data in ViewControllerA
// Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is: ", isGranted)
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK:Method for receiving Data through Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true
// Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is: ", isGranted)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
// MARK: Variable for Passing Data through Block
var authorizationCompletionBlock:((Bool)->())? = {_ in}
override func viewDidLoad() {
super.viewDidLoad()
// MARK: Set data for Passing Data through Block
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
}
}
You can find complete sample Application at my GitHub Please let me know if you have any question(s) on this.
If you want to pass data from one controller to other, try this code:
File FirstViewController.h
#property (nonatomic, retain) NSString *str;
SecondViewController.h
#property (nonatomic, retain) NSString *str1;
File FirstViewController.m
- (void)viewDidLoad
{
// Message for the second SecondViewController
self.str = #"text message";
[super viewDidLoad];
}
-(IBAction)ButtonClicked
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
}
This is a very old answer and this is anti pattern. Please use delegates. Do not use this approach!!
1. Create the instance of the first view controller in the second view controller and make its property #property (nonatomic,assign).
2. Assign the SecondviewController instance of this view controller.
2. When you finish the selection operation, copy the array to the first View Controller. When you unload the second view, the first view will hold the array data.
I was searching this solution for long time, and at last I found it. First of all, declare all the objects in your SecondViewController.h file like
#interface SecondViewController: UIviewController
{
NSMutableArray *myAray;
CustomObject *object;
}
Now in your implementation file, allocate the memory for those objects like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
myAray=[[NSMutableArray alloc] init];
object=[[CustomObject alloc] init];
}
return self;
}
Now you have allocated the memory for Array and object. Now you can fill that memory before pushing this ViewController.
Go to your SecondViewController.h and write two methods:
-(void)setMyArray:(NSArray *)_myArray;
-(void)setMyObject:(CustomObject *)_myObject;
In the implementation file, you can implement the function:
-(void)setMyArray:(NSArray *)_myArray
{
[myArra addObjectsFromArray:_myArray];
}
-(void)setMyObject:(CustomObject *)_myObject
{
[object setCustomObject:_myObject];
}
Expecting that your CustomObject must have a setter function with it.
Now your basic work is done. Go to the place where you want to push the SecondViewController and do the following stuff:
SecondViewController *secondView= [[SecondViewController alloc] initWithNibName:#"SecondViewController " bundle:[NSBundle MainBundle]] ;
[secondView setMyArray:ArrayToPass];
[secondView setMyObject:objectToPass];
[self.navigationController pushViewController:secondView animated:YES ];
Take care for spelling mistakes.
This is not the way to do it. You should use delegates.
I'll assume we have two view controllers, ViewController1 and ViewController2, and this check thing is in the first one and when its state changes, you want to do something in ViewController2. To achieve that in the proper way, you should do the below:
Add a new file to your project (Objective-C Protocol) menu File → New. Now name it ViewController1Delegate or whatever you want and write these between the #interface and #end directives:
#optional
- (void)checkStateDidChange:(BOOL)checked;
Now go to ViewController2.h and add:
#import "ViewController1Delegate.h"
Then change its definition to:
#interface ViewController2: UIViewController<ViewController1Delegate>
Now go to ViewController2.m and inside the implementation add:
- (void)checkStateDidChange:(BOOL)checked {
if (checked) {
// Do whatever you want here
NSLog(#"Checked");
}
else {
// Also do whatever you want here
NSLog(#"Not checked");
}
}
Now go to ViewController1.h and add the following property:
#property (weak, nonatomic) id<ViewController1Delegate> delegate;
Now if you are creating ViewController1 inside ViewController2 after some event, then you should do it this way using NIB files:
ViewController1* controller = [[NSBundle mainBundle] loadNibNamed:#"ViewController1" owner:self options:nil][0];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
Now you are all set. Whenever you detect the event of check changed in ViewController1, all you have to do is the below:
[delegate checkStateDidChange:checked]; // You pass here YES or NO based on the check state of your control
If you want to send data from one to another viewController, here's a way to do it:
Say we have viewControllers: viewControllerA and viewControllerB
Now in file viewControllerB.h
#interface viewControllerB : UIViewController {
NSString *string;
NSArray *array;
}
- (id)initWithArray:(NSArray)a andString:(NSString)s;
In file viewControllerB.m:
#import "viewControllerB.h"
#implementation viewControllerB
- (id)initWithArray:(NSArray)a andString:(NSString)s {
array = [[NSArray alloc] init];
array = a;
string = [[NSString alloc] init];
string = s;
}
In file viewControllerA.m:
#import "viewControllerA.h"
#import "viewControllerB.h"
#implementation viewControllerA
- (void)someMethod {
someArray = [NSArray arrayWithObjects:#"One", #"Two", #"Three", nil];
someString = [NSString stringWithFormat:#"Hahahahaha"];
viewControllerB *vc = [[viewControllerB alloc] initWithArray:someArray andString:someString];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
So this is how you can pass data from viewControllerA to viewControllerB without setting any delegate. ;)
With a Swift slant and want a bare-bones example, here is my go-to method for passing data if you are using a segue to get around.
It is similar to the above but without the buttons, labels and such. Just simply passing data from one view to the next.
Setup The Storyboard
There are three parts.
The Sender
The Segue
The Receiver
This is a very simple view layout with a segue between them.
Here is the setup for the sender
Here is the setup for the receiver.
Lastly, the setup for the segue.
The View Controllers
We are keeping this simple so no buttons and not actions. We are simply moving data from the sender to the receiver when the application loads and then outputting the transmitted value to the console.
This page takes the initially loaded value and passes it along.
import UIKit
class ViewControllerSender: UIViewController {
// THE STUFF - put some information into a variable
let favoriteMovie = "Ghost Busters"
override func viewDidAppear(animated: Bool) {
// PASS IDENTIFIER - go to the receiving view controller.
self.performSegueWithIdentifier("goToReciever", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// GET REFERENCE - ...to the receiver view.
var viewControllerReceiver = segue.destinationViewController as? ViewControllerReceiver
// PASS STUFF - pass the variable along to the target.
viewControllerReceiver!.yourFavMovie = self.favoriteMovie
}
}
This page just sends the value of the variable to the console when it loads. By this point, our favorite movie should be in that variable.
import UIKit
class ViewControllerReceiver: UIViewController {
// Basic empty variable waiting for you to pass in your fantastic favorite movie.
var yourFavMovie = ""
override func viewDidLoad() {
super.viewDidLoad()
// And now we can view it in the console.
println("The Movie is \(self.yourFavMovie)")
}
}
That is how you can tackle it if you want to use a segue and you don't have your pages under a navigation controller.
Once it is run, it should switch to the receiver view automatically and pass the value from the sender to the receiver, displaying the value in the console.
In my case, I used a singleton class which could work as a global object allowing accesses to the data from almost everywhere in the app.
The first thing is to build a singleton class. Please refer to the page What should my Objective-C singleton look like?.
And to make the object globally accessible, I simply imported it in appName_Prefix.pch which was for applying import statement in every classes.
To access this object and to use it, I simply implemented a class method to return the shared instance, which contains its own variables.
Passing data between FirstViewController to SecondViewController as below
For example:
FirstViewController String value as
StrFirstValue = #"first";
So we can pass this value in the second class using the below steps:
We need to create a string object in the SecondViewController.h file
NSString *strValue;
Need to declare a property as the below declaration in the .h file
#property (strong, nonatomic) NSString *strSecondValue;
Need synthesize that value in the FirstViewController.m file below the header declaration
#synthesize strValue;
And in file FirstViewController.h:
#property (strong, nonatomic) NSString *strValue;
In FirstViewController, from which method we navigate to the second view, please write the below code in that method.
SecondViewController *secondView= [[SecondViewController alloc]
initWithNibName:#"SecondViewController " bundle:[NSBundle MainBundle]];
[secondView setStrSecondValue:StrFirstValue];
[self.navigationController pushViewController:secondView animated:YES ];
I am currently contributing to an open source solution to this problem through a project called MCViewFactory, which may be found here:
Manticore iOS View Factory
The idea is imitate Android's intent paradigm, using a global factory to manage which view you are looking at and using "intents" to switch and pass data between views. All the documentation is on the GitHub page, but here are some highlights:
You setup all your views in .XIB files and register them in the app delegate, while initializing the factory.
// Register activities
MCViewFactory *factory = [MCViewFactory sharedFactory];
// The following two lines are optional.
[factory registerView:#"YourSectionViewController"];
Now, in your view controller (VC), anytime you want to move to a new VC and pass data, you create a new intent and add data to its dictionary (savedInstanceState). Then, just set the current intent of factory:
MCIntent* intent = [MCIntent intentWithSectionName:#"YourSectionViewController"];
[intent setAnimationStyle:UIViewAnimationOptionTransitionFlipFromLeft];
[[intent savedInstanceState] setObject:#"someValue" forKey:#"yourKey"];
[[intent savedInstanceState] setObject:#"anotherValue" forKey:#"anotherKey"];
// ...
[[MCViewModel sharedModel] setCurrentSection:intent];
All of your views that conform to this need to be subclasses of MCViewController, which allow you to override the new onResume: method, allowing you access to the data you've passed in.
-(void)onResume:(MCIntent *)intent {
NSObject* someValue = [intent.savedInstanceState objectForKey:#"yourKey"];
NSObject* anotherValue = [intent.savedInstanceState objectForKey:#"anotherKey"];
// ...
// Ensure the following line is called, especially for MCSectionViewController
[super onResume:intent];
}
Create the property in the next view controller .h file and define getters and setters.
Add this property in NextVC.h on nextVC:
#property (strong, nonatomic) NSString *indexNumber;
Add
#synthesize indexNumber; in NextVC.m
And last
NextVC *vc = [[NextVC alloc]init];
vc.indexNumber = #"123";
[self.navigationController vc animated:YES];
There are tons of ways to do this and it's important to pick the right one. Probably one of the biggest architectural decisions lies on how the model code will be shared or accessed throughout the app.
I wrote a blog post about this a while back: Sharing Model Code. Here's a brief summary:
Shared data
One approach is to share pointers to the model objects between view controllers.
Brute force iteration on view controllers (in Navigation or Tab Bar Controller) to set the data
Set data in prepareForSegue (if storyboards) or init (if programmatic)
Since prepare for segue is the most common here is an example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var next = segue.destinationViewController as NextViewController
next.dataSource = dataSource
}
Independent access
Another approach is to handle a screen full of data at a time and instead of coupling the view controllers to each other couple each view controller to single data source that they can get to independently.
The most common way I've seen this done is a singleton instance. So if your singleton object was DataAccess you could do the following in the viewDidLoad method of UIViewController:
func viewDidLoad() {
super.viewDidLoad()
var data = dataAccess.requestData()
}
There are addition tools that also help pass along data:
Key-Value Observing
NSNotification
Core Data
NSFetchedResultsController
Data Source
Core Data
The nice thing about Core Data is that it has inverse relationships. So if you want to just give a NotesViewController the notes object you can because it'll have an inverse relationship to something else like the notebook. If you need data on the notebook in the NotesViewController you can walk back up the object graph by doing the following:
let notebookName = note.notebook.name
Read more about this in my blog post: Sharing Model Code
If you want to pass data from ViewControlerOne to ViewControllerTwo, try these...
Do these in ViewControlerOne.h:
#property (nonatomic, strong) NSString *str1;
Do these in ViewControllerTwo.h:
#property (nonatomic, strong) NSString *str2;
Synthesize str2 in ViewControllerTwo.m:
#interface ViewControllerTwo ()
#end
#implementation ViewControllerTwo
#synthesize str2;
Do these in ViewControlerOne.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Data or string you wants to pass in ViewControllerTwo...
self.str1 = #"hello world";
}
O the buttons click event, do this:
-(IBAction)ButtonClicked
{
// Navigation on buttons click event from ViewControlerOne to ViewControlerTwo with transferring data or string..
ViewControllerTwo *objViewTwo = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerTwo"];
obj.str2 = str1;
[self.navigationController pushViewController: objViewTwo animated:YES];
}
Do these in ViewControllerTwo.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#", str2);
}
You can save data in an App delegate to access it across view controllers in your application. All you have to do is create a shared instance of an app delegate:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
For Example
If you declare a NSArray object *arrayXYZ, then you can access it in any view controller by appDelegate.arrayXYZ.
NewsViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tbl_View deselectRowAtIndexPath:indexPath animated:YES];
News *newsObj = [newstitleArr objectAtIndex:indexPath.row];
NewsDetailViewController *newsDetailView = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
newsDetailView.newsHeadlineStr = newsObj.newsHeadline;
[self.navigationController pushViewController:newsDetailView animated:YES];
}
NewsDetailViewController.h
#interface NewsDetailViewController : UIViewController
#property(nonatomic,retain) NSString *newsHeadlineStr;
#end
NewsDetailViewController.m
#synthesize newsHeadlineStr;
Delegation is the only one solution to perform such operations when you are using .xib files. However, all previous answers are for storyboard for .xibs files. You need to use delegation. That's the only solution you can use.
Another solution is use the singleton class pattern. Initialize it once and use it in your entire app.
For SwiftUI
Think of #EnvironmentObject as a smarter, simpler way of using #ObservedObject on lots of views. Rather than creating some data in view A, then passing it to view B, then view C, then view D before finally using it, you can create it in view and put it into the environment so that views B, C, and D will automatically have access to it.
Note: Environment objects must be supplied by an ancestor view – if SwiftUI can’t find an environment object of the correct type you’ll get a crash. This applies for previews too, so be careful.
As an example, here’s an observable object that stores user settings:
class UserSettings: ObservableObject {
#Published var score = 0
}
There are 3 types for passing data one ViewController to another
ViewController.
Programatically
Segue
UserDefaults
Demo Project Link Here - https://github.com/kamanijasmin13/Swift-Pass-data-between-viewcontrollers
Programatically
Segue
UserDefaults
Demo Project Link Here - https://github.com/kamanijasmin13/Swift-Pass-data-between-viewcontrollers
I like the idea of model objects and mock objects based on NSProxy to commit or discard data if what the user selects can be cancelled.
It's easy to pass data around since it's a single object or couple of objects and if you have, let's say, a UINavigationController controller, you can keep the reference to the model inside and all pushed view controllers can access it directly from the navigation controller.
I have seen a lot of people over complicating this using the didSelectRowAtPath method. I am using Core Data in my example.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// This solution is for using Core Data
YourCDEntityName * value = (YourCDEntityName *)[[self fetchedResultsController] objectAtIndexPath: indexPath];
YourSecondViewController * details = [self.storyboard instantiateViewControllerWithIdentifier:#"nameOfYourSecondVC"]; // Make sure in storyboards you give your second VC an identifier
// Make sure you declare your value in the second view controller
details.selectedValue = value;
// Now that you have said to pass value all you need to do is change views
[self.navigationController pushViewController: details animated:YES];
}
Four lines of code inside the method and you are done.

Access a property of a class from another class objective C [duplicate]

I'm new to iOS and Objective-C and the whole MVC paradigm and I'm stuck with the following:
I have a view that acts as a data entry form and I want to give the user the option to select multiple products. The products are listed on another view with a UITableViewController and I have enabled multiple selections.
How do I transfer the data from one view to another? I will be holding the selections on the UITableView in an array, but how do I then pass that back to the previous data entry form view so it can be saved along with the other data to Core Data on submission of the form?
I have surfed around and seen some people declare an array in the app delegate. I read something about singletons, but I don't understand what these are and I read something about creating a data model.
What would be the correct way of performing this and how would I go about it?
This question seems to be very popular here on Stack Overflow so I thought I would try and give a better answer to help out people starting in the world of iOS like me.
Passing Data Forward
Passing data forward to a view controller from another view controller. You would use this method if you wanted to pass an object/value from one view controller to another view controller that you may be pushing on to a navigation stack.
For this example, we will have ViewControllerA and ViewControllerB
To pass a BOOL value from ViewControllerA to ViewControllerB we would do the following.
in ViewControllerB.h create a property for the BOOL
#property (nonatomic, assign) BOOL isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB so use an
#import "ViewControllerB.h"
Then where you want to load the view, for example, didSelectRowAtIndex or some IBAction, you need to set the property in ViewControllerB before you push it onto the navigation stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:#"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
Passing Data Forward using Segues
If you are using Storyboards you are most likely using segues and will need this procedure to pass data forward. This is similar to the above but instead of passing the data before you push the view controller, you use a method called
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
So to pass a BOOL from ViewControllerA to ViewControllerB we would do the following:
in ViewControllerB.h create a property for the BOOL
#property (nonatomic, assign) BOOL isSomethingEnabled;
in ViewControllerA you need to tell it about ViewControllerB, so use an
#import "ViewControllerB.h"
Create the segue from ViewControllerA to ViewControllerB on the storyboard and give it an identifier. In this example we'll call it "showDetailSegue"
Next, we need to add the method to ViewControllerA that is called when any segue is performed. Because of this we need to detect which segue was called and then do something. In our example, we will check for "showDetailSegue" and if that's performed, we will pass our BOOL value to ViewControllerB
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showDetailSegue"]){
ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.isSomethingEnabled = YES;
}
}
If you have your views embedded in a navigation controller, you need to change the method above slightly to the following
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showDetailSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
controller.isSomethingEnabled = YES;
}
}
This will set isSomethingEnabled in ViewControllerB to BOOL value YES.
Passing Data Back
To pass data back from ViewControllerB to ViewControllerA you need to use Protocols and Delegates or Blocks, the latter can be used as a loosely coupled mechanism for callbacks.
To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
For ViewControllerA to be a delegate of ViewControllerB it must conform to ViewControllerB's protocol which we have to specify. This tells ViewControllerA which methods it must implement.
In ViewControllerB.h, below the #import, but above #interface you specify the protocol.
#class ViewControllerB;
#protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
#end
Next still in the ViewControllerB.h, you need to set up a delegate property and synthesize in ViewControllerB.m
#property (nonatomic, weak) id <ViewControllerBDelegate> delegate;
In ViewControllerB we call a message on the delegate when we pop the view controller.
NSString *itemToPassBack = #"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.
#import "ViewControllerB.h"
#interface ViewControllerA : UIViewController <ViewControllerBDelegate>
In ViewControllerA.m implement the following method from our protocol
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
NSLog(#"This was returned from ViewControllerB %#", item);
}
Before pushing viewControllerB to navigation stack we need to tell ViewControllerB that ViewControllerA is its delegate, otherwise we will get an error.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:#"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];
References
Using Delegation to Communicate With Other View Controllers in the View Controller Programming Guide
Delegate Pattern
NSNotification center
It's another way to pass data.
// Add an observer in controller(s) where you want to receive data
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleDeepLinking:) name:#"handleDeepLinking" object:nil];
-(void) handleDeepLinking:(NSNotification *) notification {
id someObject = notification.object // Some custom object that was passed with notification fire.
}
// Post notification
id someObject;
[NSNotificationCenter.defaultCenter postNotificationName:#"handleDeepLinking" object:someObject];
Passing Data back from one class to another (A class can be any controller, Network/session manager, UIView subclass or any other class)
Blocks are anonymous functions.
This example passes data from Controller B to Controller A
Define a block
#property void(^selectedVoucherBlock)(NSString *); // in ContollerA.h
Add block handler (listener)
Where you need a value (for example, you need your API response in ControllerA or you need ContorllerB data on A)
// In ContollerA.m
- (void)viewDidLoad {
[super viewDidLoad];
__unsafe_unretained typeof(self) weakSelf = self;
self.selectedVoucherBlock = ^(NSString *voucher) {
weakSelf->someLabel.text = voucher;
};
}
Go to Controller B
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ControllerB *vc = [storyboard instantiateViewControllerWithIdentifier:#"ControllerB"];
vc.sourceVC = self;
[self.navigationController pushViewController:vc animated:NO];
Fire block
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSString *voucher = vouchersArray[indexPath.row];
if (sourceVC.selectVoucherBlock) {
sourceVC.selectVoucherBlock(voucher);
}
[self.navigationController popToViewController:sourceVC animated:YES];
}
Another Working Example for Blocks
Swift
There are tons and tons of explanations here and around Stack Overflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it).
YouTube tutorial: How to send data through segue (Swift)
Passing data forward to the next View Controller
The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
Create the storyboard layout in the Interface Builder. To make the segue, you just Control click on the button and drag over to the Second View Controller.
First View Controller
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get a reference to the second view controller
let secondViewController = segue.destination as! SecondViewController
// Set a variable in the second view controller with the String to pass
secondViewController.receivedString = textField.text!
}
}
Second View Controller
And the code for the Second View Controller is
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
// This variable will hold the data being passed from the First View Controller
var receivedString = ""
override func viewDidLoad() {
super.viewDidLoad()
// Used the text from the First View Controller to set the label
label.text = receivedString
}
}
Don't forget
Hook up the outlets for the UITextField and the UILabel.
Set the first and second View Controllers to the appropriate Swift files in Interface Builder.
Passing data back to the previous View Controller
To pass data back from the second view controller to the first view controller, you use a protocol and a delegate. This video is a very clear walk though of that process:
YouTube tutorial: iOS Swift Basics Tutorial: Protocols and Delegates But also read this post to make sure you don't get into a strong reference cycle.
The following is an example based on the video (with a few modifications).
Create the storyboard layout in the Interface Builder. Again, to make the segue, you just Control drag from the button to the Second View Controller. Set the segue identifier to showSecondViewController. Also, don't forget to hook up the outlets and actions using the names in the following code.
First View Controller
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController, DataEnteredDelegate {
#IBOutlet weak var label: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
let secondViewController = segue.destination as! SecondViewController
secondViewController.delegate = self
}
}
func userDidEnterInformation(info: String) {
label.text = info
}
}
Note the use of our custom DataEnteredDelegate protocol.
Second View Controller and Protocol
The code for the second view controller is
import UIKit
// Protocol used for sending data back
protocol DataEnteredDelegate: AnyObject {
func userDidEnterInformation(info: String)
}
class SecondViewController: UIViewController {
// Making this a weak variable, so that it won't create a strong reference cycle
weak var delegate: DataEnteredDelegate? = nil
#IBOutlet weak var textField: UITextField!
#IBAction func sendTextBackButton(sender: AnyObject) {
// Call this method on whichever class implements our delegate protocol
delegate?.userDidEnterInformation(info: textField.text!)
// Go back to the previous view controller
_ = self.navigationController?.popViewController(animated: true)
}
}
Note that the protocol is outside of the View Controller class.
That's it. Running the app now, you should be able to send data back from the second view controller to the first.
The M in MVC is for "Model" and in the MVC paradigm the role of model classes is to manage a program's data. A model is the opposite of a view -- a view knows how to display data, but it knows nothing about what to do with data, whereas a model knows everything about how to work with data, but nothing about how to display it. Models can be complicated, but they don't have to be -- the model for your app might be as simple as an array of strings or dictionaries.
The role of a controller is to mediate between view and model. Therefore, they need a reference to one or more view objects and one or more model objects. Let's say that your model is an array of dictionaries, with each dictionary representing one row in your table. The root view for your app displays that table, and it might be responsible for loading the array from a file. When the user decides to add a new row to the table, they tap some button and your controller creates a new (mutable) dictionary and adds it to the array. In order to fill in the row, the controller creates a detail view controller and gives it the new dictionary. The detail view controller fills in the dictionary and returns. The dictionary is already part of the model, so nothing else needs to happen.
There are various ways by which data can be received by a different class in iOS. For example -
Direct initialization after the allocation of another class.
Delegation - for passing data back
Notification - for broadcasting data to multiple classes at a single time
Saving in NSUserDefaults - for accessing it later
Singleton classes
Databases and other storage mechanisms, like p-list files, etc.
But for the simple scenario of passing a value to a different class whose allocation is done in the current class, the most common and preferred method would be the direct setting of values after allocation. This is done as follows:
We can understand it using two controllers - Controller1 and Controller2
Suppose in Controller1 class you want to create the Controller2 object and push it with a String value being passed. This can be done as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passValue:#"String"];
[self pushViewController:obj animated:YES];
}
In the implementation of the Controller2 class there will be this function as:
#interface Controller2 : NSObject
#property (nonatomic, strong) NSString* stringPassed;
#end
#implementation Controller2
#synthesize stringPassed = _stringPassed;
- (void) passValue:(NSString *)value {
_stringPassed = value; // Or self.stringPassed = value
}
#end
You can also directly set the properties of the Controller2 class in the similar way as this:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj setStringPassed:#"String"];
[self pushViewController:obj animated:YES];
}
To pass multiple values, you can use the multiple parameters like:
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passValue:#“String1” andValues:objArray withDate:date];
Or if you need to pass more than three parameters which are related to a common feature, you can store the values in a model class and pass that modelObject to the next class
ModelClass *modelObject = [[ModelClass alloc] init];
modelObject.property1 = _property1;
modelObject.property2 = _property2;
modelObject.property3 = _property3;
Controller2 *obj = [[Controller2 alloc] initWithNib:#"Controller2" bundle:nil];
[obj passmodel: modelObject];
So in short, if you want to -
set the private variables of the second class initialise the values by calling a custom function and passing the values.
setProperties do it by directlyInitialising it using the setter method.
pass more that 3-4 values related to each other in some manner, then create a model class and set values to its object and pass the object using any of the above process.
After more research it seemed that protocols and delegates were the correct/Apple preferred way of doing this.
I ended up using this example (in the iPhone development SDK):
Sharing data between view controllers and other objects
It worked fine and allowed me to pass a string and an array forward and back between my views.
I find simplest and most elegant version with passing blocks.
Let's name view controller that waits for returned data as "A" and returning view controller as "B". In this example we want to get 2 values: first of Type1 and second of Type2.
Assuming we use Storyboard, first controller sets callback block, for example during segue preparation:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[BViewController class]])
{
BViewController *viewController = segue.destinationViewController;
viewController.callback = ^(Type1 *value1, Type2 *value2) {
// optionally, close B
//[self.navigationController popViewControllerAnimated:YES];
// let's do some action after with returned values
action1(value1);
action2(value2);
};
}
}
and "B" view controller should declare callback property, BViewController.h:
// it is important to use "copy"
#property (copy) void(^callback)(Type1 *value1, Type2 *value2);
Than in implementation file BViewController.m after we have desired values to return our callback should be called:
if (self.callback)
self.callback(value1, value2);
One thing to remember is that using block often needs to manage strong and __weak references like explained here
There is some good information in many of the answers given, but none address the question fully.
The question asks about passing information between view controllers. The specific example given asks about passing information between views, but given the self-stated newness to iOS, the original poster likely meant between viewControllers, not between views (without any involvement from the ViewControllers). It seems that all the answers focus on two view controllers, but what if the app evolves to need to involve more than two view controllers in the information exchange?
The original poster also asked about Singletons and the use of the AppDelegate. These questions need to be answered.
To help anyone else looking at this question, who wants a full answer, I'm going to attempt to provide it.
Application Scenarios
Rather than having a highly hypothetical, abstract discussion, it helps to have concrete applications in mind. To help define a two-view-controller situation and a more-than-two-view-controller situation, I am going to define two concrete application scenarios.
Scenario one: maximum two view controllers ever need to share information.
See diagram one.
There are two view controllers in the application. There is a ViewControllerA (Data Entry Form), and View Controller B (Product List). The items selected in the product list must match the items displayed in the text box in the data entry form. In this scenario, ViewControllerA and ViewControllerB must communicate directly with each other and no other view controllers.
Scenario two: more than two view controllers need to share the same information.
See diagram two.
There are four view controllers in the application. It is a tab-based application for managing home inventory. Three view controllers present differently filtered views of the same data:
ViewControllerA - Luxury Items
ViewControllerB - Non-insured Items
ViewControllerC - Entire Home Inventory
ViewControllerD - Add New Item Form
Any time an individual item is created or edited, it must also synchronize with the other view controllers. For example, if we add a boat in ViewControllerD, but it is not yet insured, then the boat must appear when the user goes to ViewControllerA (Luxury Items), and also ViewControllerC (Entire Home Inventory), but not when the user goes to ViewControllerB (Non-insured Items). We need be concerned with not only adding new items, but also deleting items (which may be allowed from any of the four view controllers), or editing existing items (which may be allowed from the "Add New Item Form", repurposing the same for editing).
Since all the view controllers do need to share the same data, all four view controllers need to remain in synchronization, and therefore there needs to be some sort of communication to all other view controllers, whenever any single view controller changes the underlying data. It should be fairly obvious that we do not want each view controller communicating directly with each other view controller in this scenario. In case it is not obvious, consider if we had 20 different view controllers (rather than just 4). How difficult and error-prone would it be to notify each of the other 19 view controllers any time one view controller made a change?
The Solutions: Delegates and the Observer Pattern, and Singletons
In scenario one, we have several viable solutions, as other answers have given
segues
delegates
setting properties on view controllers directly
NSUserDefaults (actually a poor choice)
In scenario two, we have other viable solutions:
Observer Pattern
Singletons
A singleton is an instance of a class, that instance being the only instance in existence during its lifetime. A singleton gets its name from the fact that it is the single instance. Normally developers who use singletons have special class methods for accessing them.
+ (HouseholdInventoryManager*) sharedManager; {
static dispatch_once_t onceQueue;
static HouseholdInventoryManager* _sharedInstance;
// dispatch_once is guaranteed to only be executed
// once in the lifetime of the application
dispatch_once(&onceQueue, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
Now that we understand what a singleton is, let's discuss how a singleton fits into the observer pattern. The observer pattern is used for one object to respond to changes by another object. In the second scenario, we have four different view controllers, who all want to know about changes to the underlying data. The "underlying data" should belong to a single instance, a singleton. The "know about changes" is accomplished by observing changes made to the singleton.
The home inventory application would have a single instance of a class which is designed to manage a list of inventory items. The manager would manage a collection of household items. The following is a class definition for the data manager:
#import <Foundation/Foundation.h>
#class JGCHouseholdInventoryItem;
#interface HouseholdInventoryManager : NSObject
/*!
The global singleton for accessing application data
*/
+ (HouseholdInventoryManager*) sharedManager;
- (NSArray *) entireHouseholdInventory;
- (NSArray *) luxuryItems;
- (NSArray *) nonInsuredItems;
- (void) addHouseholdItemToHomeInventory:(JGCHouseholdInventoryItem*)item;
- (void) editHouseholdItemInHomeInventory:(JGCHouseholdInventoryItem*)item;
- (void) deleteHoueholdItemFromHomeInventory:(JGCHouseholdInventoryItem*)item;
#end
When the collection of home inventory items changes, the view controllers need to be made aware of this change. The class definition above does not make it obvious how this will happen. We need to follow the observer pattern. The view controllers must formally observe the sharedManager. There are two ways to observe another object:
Key-Value-Observing (KVO)
NSNotificationCenter.
In scenario two, we do not have a single property of the HouseholdInventoryManager which could be observed using KVO. Because we do not have a single property which is easily observable, the observer pattern, in this case, must be implemented using NSNotificationCenter. Each of the four view controllers would subscribe to notifications, and the sharedManager would send notifications to the notification center when appropriate. The inventory manager does not need to know anything about the view controllers or instances of any other classes which may be interested in knowing when the collection of inventory items changes; the NSNotificationCenter takes care of these implementation details. The View Controllers simply subscribe to notifications, and the data manager simply posts notifications.
Many beginner programmers take advantage of the fact that there is always exactly one Application Delegate in the lifetime of the application, which is globally accessible. Beginning programmers use this fact to stuff objects and functionality into the appDelegate as a convenience for access from anywhere else in the application. Just because the AppDelegate is a singleton doesn't mean it should replace all other singletons. This is a poor practice as it places too much burden on one class, breaking good object-oriented practices. Each class should have a clear role that is easily explained, often just by the name of the class.
Any time your Application Delegate starts to get bloated, start to remove functionality into singletons. For example, the Core Data Stack should not be left in the AppDelegate, but should instead be put in its own class, a coreDataManager class.
References
Managing Data Flow Between View Controllers
Passing Data Between View Controllers
Asynchronous JSON Requests in Objective-C
Passing data back from ViewController 2 (destination) to viewController 1 (source) is the more interesting thing.
Assuming you use storyBoard, these are all the ways I found out:
Delegate
Notification
User defaults
Singleton
Those were discussed here already.
I found there are more ways:
Using Block callbacks:
Use it in the prepareForSegue method in the VC1
NextViewController *destinationVC = (NextViewController *) segue.destinationViewController;
[destinationVC setDidFinishUsingBlockCallback:^(NextViewController *destinationVC)
{
self.blockLabel.text = destination.blockTextField.text;
}];
Using storyboards Unwind (Exit)
Implement a method with a UIStoryboardSegue argument in VC 1,like this one:
-(IBAction)UnWindDone:(UIStoryboardSegue *)segue { }
In the storyBoard, hook the "return" button to the green Exit button (Unwind) of the vc. Now you have a segue that "goes back" so you can use the destinationViewController property in the prepareForSegue of VC2 and
change any property of VC1 before it goes back.
Another option of using storyboards Undwind (Exit) - you can use the method you wrote in VC1
-(IBAction)UnWindDone:(UIStoryboardSegue *)segue {
NextViewController *nextViewController = segue.sourceViewController;
self.unwindLabel.text = nextViewController.unwindPropertyPass;
}
And in the prepareForSegue of VC1 you can change any property you want to share.
In both unwind options, you can set the tag property of the button and check it in the prepareForSegue.
The OP didn't mention view controllers but so many of the answers do, that I wanted to chime in with what some of the new features of the LLVM allow to make this easier when wanting to pass data from one view controller to another and then getting some results back.
Storyboard segues, ARC and LLVM blocks make this easier than ever for me. Some answers above mentioned storyboards and segues already but still relied on delegation. Defining delegates certainly works but some people may find it easier to pass pointers or code blocks.
With UINavigators and segues, there are easy ways of passing information to the subservient controller and getting the information back. ARC makes passing pointers to things derived from NSObjects simple so if you want the subservient controller to add/change/modify some data for you, pass it a pointer to a mutable instance. Blocks make passing actions easy so if you want the subservient controller to invoke an action on your higher level controller, pass it a block. You define the block to accept any number of arguments that makes sense to you. You can also design the API to use multiple blocks if that suits things better.
Here are two trivial examples of the segue glue. The first is straightforward showing one parameter passed for input, the second for output.
// Prepare the destination view controller by passing it the input we want it to work on
// and the results we will look at when the user has navigated back to this controller's view.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[[segue destinationViewController]
// This parameter gives the next controller the data it works on.
segueHandoffWithInput:self.dataForNextController
// This parameter allows the next controller to pass back results
// by virtue of both controllers having a pointer to the same object.
andResults:self.resultsFromNextController];
}
This second example shows passing a callback block for the second argument. I like using blocks because it keeps the relevant details close together in the source - the higher level source.
// Prepare the destination view controller by passing it the input we want it to work on
// and the callback when it has done its work.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[[segue destinationViewController]
// This parameter gives the next controller the data it works on.
segueHandoffWithInput:self.dataForNextController
// This parameter allows the next controller to pass back results.
resultsBlock:^(id results) {
// This callback could be as involved as you like.
// It can use Grand Central Dispatch to have work done on another thread for example.
[self setResultsFromNextController:results];
}];
}
There are multiple methods for sharing data.
You can always share data using NSUserDefaults. Set the value you want to share with respect to a key of your choice and get the value from NSUserDefault associated to that key in the next view controller.
[[NSUserDefaults standardUserDefaults] setValue:value forKey:key]
[[NSUserDefaults standardUserDefaults] objectForKey:key]
You can just create a property in viewcontrollerA. Create an object of viewcontrollerA in viewcontrollerB and assign the desired value to that property.
You can also create custom delegates for this.
Swift 5
Well Matt Price's answer is perfectly fine for passing data, but I
am going to rewrite it, in the latest Swift version because I believe new
programmers find it quit challenging due to new syntax and
methods/frameworks, as original post is in Objective-C.
There are multiple options for passing data between view controllers.
Using Navigation Controller Push
Using Segue
Using Delegate
Using Notification Observer
Using Block
I am going to rewrite his logic in Swift with the latest iOS framework
Passing Data through Navigation Controller Push: From ViewControllerA to ViewControllerB
Step 1. Declare variable in ViewControllerB
var isSomethingEnabled = false
Step 2. Print Variable in ViewControllerB' ViewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through segue, navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
Step 3. In ViewControllerA Pass Data while pushing through Navigation Controller
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: Passing data through navigation PushViewController
#IBAction func goToViewControllerB(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
// MARK: - Variable for Passing Data through Navigation push
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
}
Passing Data through Segue: From ViewControllerA to ViewControllerB
Step 1. Create Segue from ViewControllerA to ViewControllerB and give Identifier = showDetailSegue in Storyboard as shown below
Step 2. In ViewControllerB Declare a viable named isSomethingEnabled and print its value.
Step 3. In ViewControllerA pass isSomethingEnabled's value while passing Segue
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - - Passing Data through Segue - -
#IBAction func goToViewControllerBUsingSegue(_ sender: Any) {
performSegue(withIdentifier: "showDetailSegue", sender: nil)
}
// Segue Delegate Method
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true//passing data
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
// Print value received through segue
print("Value of 'isSomethingEnabled' from ViewControllerA: ", isSomethingEnabled)
}
}
Passing Data through Delegate: From ViewControllerB to ViewControllerA
Step 1. Declare Protocol ViewControllerBDelegate in the ViewControllerB file, but outside the class
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
Step 2. Declare Delegate variable instance in ViewControllerB
var delegate: ViewControllerBDelegate?
Step 3. Send data for delegate inside viewDidLoad method of ViewControllerB
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
Step 4. Confirm ViewControllerBDelegate in ViewControllerA
class ViewControllerA: UIViewController, ViewControllerBDelegate {
// to do
}
Step 5. Confirm that you will implement a delegate in ViewControllerA
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self//confirming delegate
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
Step 6. Implement delegate method for receiving data in ViewControllerA
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController, ViewControllerBDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
// Delegate method
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
#IBAction func goToViewControllerForDelegate(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
//Protocol decleare
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
class ViewControllerB: UIViewController {
var delegate: ViewControllerBDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// MARK: - - - - Set Data for Passing Data through Delegate - - - - - -
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
}
}
Passing Data through Notification Observer: From ViewControllerB to ViewControllerA
Step 1. Set and post data in the notification observer in ViewControllerB
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
Step 2. Add Notification Observer in ViewControllerA
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
Step 3. Receive Notification data value in ViewControllerA
#objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification: ", notification.object ?? "")
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Add observer in controller(s) where you want to receive data
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
}
// MARK: Method for receiving Data through Post Notification
#objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification: ", notification.object ?? "")
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// MARK:Set data for Passing Data through Post Notification
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
}
}
Passing Data through Block: From ViewControllerB to ViewControllerA
Step 1. Declare block in ViewControllerB
var authorizationCompletionBlock:((Bool)->())? = {_ in}
Step 2. Set data in block in ViewControllerB
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
Step 3. Receive block data in ViewControllerA
// Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is: ", isGranted)
}
So here is the complete code for:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK:Method for receiving Data through Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true
// Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is: ", isGranted)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
// MARK: Variable for Passing Data through Block
var authorizationCompletionBlock:((Bool)->())? = {_ in}
override func viewDidLoad() {
super.viewDidLoad()
// MARK: Set data for Passing Data through Block
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
}
}
You can find complete sample Application at my GitHub Please let me know if you have any question(s) on this.
If you want to pass data from one controller to other, try this code:
File FirstViewController.h
#property (nonatomic, retain) NSString *str;
SecondViewController.h
#property (nonatomic, retain) NSString *str1;
File FirstViewController.m
- (void)viewDidLoad
{
// Message for the second SecondViewController
self.str = #"text message";
[super viewDidLoad];
}
-(IBAction)ButtonClicked
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
}
This is a very old answer and this is anti pattern. Please use delegates. Do not use this approach!!
1. Create the instance of the first view controller in the second view controller and make its property #property (nonatomic,assign).
2. Assign the SecondviewController instance of this view controller.
2. When you finish the selection operation, copy the array to the first View Controller. When you unload the second view, the first view will hold the array data.
I was searching this solution for long time, and at last I found it. First of all, declare all the objects in your SecondViewController.h file like
#interface SecondViewController: UIviewController
{
NSMutableArray *myAray;
CustomObject *object;
}
Now in your implementation file, allocate the memory for those objects like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
myAray=[[NSMutableArray alloc] init];
object=[[CustomObject alloc] init];
}
return self;
}
Now you have allocated the memory for Array and object. Now you can fill that memory before pushing this ViewController.
Go to your SecondViewController.h and write two methods:
-(void)setMyArray:(NSArray *)_myArray;
-(void)setMyObject:(CustomObject *)_myObject;
In the implementation file, you can implement the function:
-(void)setMyArray:(NSArray *)_myArray
{
[myArra addObjectsFromArray:_myArray];
}
-(void)setMyObject:(CustomObject *)_myObject
{
[object setCustomObject:_myObject];
}
Expecting that your CustomObject must have a setter function with it.
Now your basic work is done. Go to the place where you want to push the SecondViewController and do the following stuff:
SecondViewController *secondView= [[SecondViewController alloc] initWithNibName:#"SecondViewController " bundle:[NSBundle MainBundle]] ;
[secondView setMyArray:ArrayToPass];
[secondView setMyObject:objectToPass];
[self.navigationController pushViewController:secondView animated:YES ];
Take care for spelling mistakes.
This is not the way to do it. You should use delegates.
I'll assume we have two view controllers, ViewController1 and ViewController2, and this check thing is in the first one and when its state changes, you want to do something in ViewController2. To achieve that in the proper way, you should do the below:
Add a new file to your project (Objective-C Protocol) menu File → New. Now name it ViewController1Delegate or whatever you want and write these between the #interface and #end directives:
#optional
- (void)checkStateDidChange:(BOOL)checked;
Now go to ViewController2.h and add:
#import "ViewController1Delegate.h"
Then change its definition to:
#interface ViewController2: UIViewController<ViewController1Delegate>
Now go to ViewController2.m and inside the implementation add:
- (void)checkStateDidChange:(BOOL)checked {
if (checked) {
// Do whatever you want here
NSLog(#"Checked");
}
else {
// Also do whatever you want here
NSLog(#"Not checked");
}
}
Now go to ViewController1.h and add the following property:
#property (weak, nonatomic) id<ViewController1Delegate> delegate;
Now if you are creating ViewController1 inside ViewController2 after some event, then you should do it this way using NIB files:
ViewController1* controller = [[NSBundle mainBundle] loadNibNamed:#"ViewController1" owner:self options:nil][0];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:nil];
Now you are all set. Whenever you detect the event of check changed in ViewController1, all you have to do is the below:
[delegate checkStateDidChange:checked]; // You pass here YES or NO based on the check state of your control
If you want to send data from one to another viewController, here's a way to do it:
Say we have viewControllers: viewControllerA and viewControllerB
Now in file viewControllerB.h
#interface viewControllerB : UIViewController {
NSString *string;
NSArray *array;
}
- (id)initWithArray:(NSArray)a andString:(NSString)s;
In file viewControllerB.m:
#import "viewControllerB.h"
#implementation viewControllerB
- (id)initWithArray:(NSArray)a andString:(NSString)s {
array = [[NSArray alloc] init];
array = a;
string = [[NSString alloc] init];
string = s;
}
In file viewControllerA.m:
#import "viewControllerA.h"
#import "viewControllerB.h"
#implementation viewControllerA
- (void)someMethod {
someArray = [NSArray arrayWithObjects:#"One", #"Two", #"Three", nil];
someString = [NSString stringWithFormat:#"Hahahahaha"];
viewControllerB *vc = [[viewControllerB alloc] initWithArray:someArray andString:someString];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
So this is how you can pass data from viewControllerA to viewControllerB without setting any delegate. ;)
With a Swift slant and want a bare-bones example, here is my go-to method for passing data if you are using a segue to get around.
It is similar to the above but without the buttons, labels and such. Just simply passing data from one view to the next.
Setup The Storyboard
There are three parts.
The Sender
The Segue
The Receiver
This is a very simple view layout with a segue between them.
Here is the setup for the sender
Here is the setup for the receiver.
Lastly, the setup for the segue.
The View Controllers
We are keeping this simple so no buttons and not actions. We are simply moving data from the sender to the receiver when the application loads and then outputting the transmitted value to the console.
This page takes the initially loaded value and passes it along.
import UIKit
class ViewControllerSender: UIViewController {
// THE STUFF - put some information into a variable
let favoriteMovie = "Ghost Busters"
override func viewDidAppear(animated: Bool) {
// PASS IDENTIFIER - go to the receiving view controller.
self.performSegueWithIdentifier("goToReciever", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// GET REFERENCE - ...to the receiver view.
var viewControllerReceiver = segue.destinationViewController as? ViewControllerReceiver
// PASS STUFF - pass the variable along to the target.
viewControllerReceiver!.yourFavMovie = self.favoriteMovie
}
}
This page just sends the value of the variable to the console when it loads. By this point, our favorite movie should be in that variable.
import UIKit
class ViewControllerReceiver: UIViewController {
// Basic empty variable waiting for you to pass in your fantastic favorite movie.
var yourFavMovie = ""
override func viewDidLoad() {
super.viewDidLoad()
// And now we can view it in the console.
println("The Movie is \(self.yourFavMovie)")
}
}
That is how you can tackle it if you want to use a segue and you don't have your pages under a navigation controller.
Once it is run, it should switch to the receiver view automatically and pass the value from the sender to the receiver, displaying the value in the console.
In my case, I used a singleton class which could work as a global object allowing accesses to the data from almost everywhere in the app.
The first thing is to build a singleton class. Please refer to the page What should my Objective-C singleton look like?.
And to make the object globally accessible, I simply imported it in appName_Prefix.pch which was for applying import statement in every classes.
To access this object and to use it, I simply implemented a class method to return the shared instance, which contains its own variables.
Passing data between FirstViewController to SecondViewController as below
For example:
FirstViewController String value as
StrFirstValue = #"first";
So we can pass this value in the second class using the below steps:
We need to create a string object in the SecondViewController.h file
NSString *strValue;
Need to declare a property as the below declaration in the .h file
#property (strong, nonatomic) NSString *strSecondValue;
Need synthesize that value in the FirstViewController.m file below the header declaration
#synthesize strValue;
And in file FirstViewController.h:
#property (strong, nonatomic) NSString *strValue;
In FirstViewController, from which method we navigate to the second view, please write the below code in that method.
SecondViewController *secondView= [[SecondViewController alloc]
initWithNibName:#"SecondViewController " bundle:[NSBundle MainBundle]];
[secondView setStrSecondValue:StrFirstValue];
[self.navigationController pushViewController:secondView animated:YES ];
I am currently contributing to an open source solution to this problem through a project called MCViewFactory, which may be found here:
Manticore iOS View Factory
The idea is imitate Android's intent paradigm, using a global factory to manage which view you are looking at and using "intents" to switch and pass data between views. All the documentation is on the GitHub page, but here are some highlights:
You setup all your views in .XIB files and register them in the app delegate, while initializing the factory.
// Register activities
MCViewFactory *factory = [MCViewFactory sharedFactory];
// The following two lines are optional.
[factory registerView:#"YourSectionViewController"];
Now, in your view controller (VC), anytime you want to move to a new VC and pass data, you create a new intent and add data to its dictionary (savedInstanceState). Then, just set the current intent of factory:
MCIntent* intent = [MCIntent intentWithSectionName:#"YourSectionViewController"];
[intent setAnimationStyle:UIViewAnimationOptionTransitionFlipFromLeft];
[[intent savedInstanceState] setObject:#"someValue" forKey:#"yourKey"];
[[intent savedInstanceState] setObject:#"anotherValue" forKey:#"anotherKey"];
// ...
[[MCViewModel sharedModel] setCurrentSection:intent];
All of your views that conform to this need to be subclasses of MCViewController, which allow you to override the new onResume: method, allowing you access to the data you've passed in.
-(void)onResume:(MCIntent *)intent {
NSObject* someValue = [intent.savedInstanceState objectForKey:#"yourKey"];
NSObject* anotherValue = [intent.savedInstanceState objectForKey:#"anotherKey"];
// ...
// Ensure the following line is called, especially for MCSectionViewController
[super onResume:intent];
}
Create the property in the next view controller .h file and define getters and setters.
Add this property in NextVC.h on nextVC:
#property (strong, nonatomic) NSString *indexNumber;
Add
#synthesize indexNumber; in NextVC.m
And last
NextVC *vc = [[NextVC alloc]init];
vc.indexNumber = #"123";
[self.navigationController vc animated:YES];
There are tons of ways to do this and it's important to pick the right one. Probably one of the biggest architectural decisions lies on how the model code will be shared or accessed throughout the app.
I wrote a blog post about this a while back: Sharing Model Code. Here's a brief summary:
Shared data
One approach is to share pointers to the model objects between view controllers.
Brute force iteration on view controllers (in Navigation or Tab Bar Controller) to set the data
Set data in prepareForSegue (if storyboards) or init (if programmatic)
Since prepare for segue is the most common here is an example:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var next = segue.destinationViewController as NextViewController
next.dataSource = dataSource
}
Independent access
Another approach is to handle a screen full of data at a time and instead of coupling the view controllers to each other couple each view controller to single data source that they can get to independently.
The most common way I've seen this done is a singleton instance. So if your singleton object was DataAccess you could do the following in the viewDidLoad method of UIViewController:
func viewDidLoad() {
super.viewDidLoad()
var data = dataAccess.requestData()
}
There are addition tools that also help pass along data:
Key-Value Observing
NSNotification
Core Data
NSFetchedResultsController
Data Source
Core Data
The nice thing about Core Data is that it has inverse relationships. So if you want to just give a NotesViewController the notes object you can because it'll have an inverse relationship to something else like the notebook. If you need data on the notebook in the NotesViewController you can walk back up the object graph by doing the following:
let notebookName = note.notebook.name
Read more about this in my blog post: Sharing Model Code
If you want to pass data from ViewControlerOne to ViewControllerTwo, try these...
Do these in ViewControlerOne.h:
#property (nonatomic, strong) NSString *str1;
Do these in ViewControllerTwo.h:
#property (nonatomic, strong) NSString *str2;
Synthesize str2 in ViewControllerTwo.m:
#interface ViewControllerTwo ()
#end
#implementation ViewControllerTwo
#synthesize str2;
Do these in ViewControlerOne.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Data or string you wants to pass in ViewControllerTwo...
self.str1 = #"hello world";
}
O the buttons click event, do this:
-(IBAction)ButtonClicked
{
// Navigation on buttons click event from ViewControlerOne to ViewControlerTwo with transferring data or string..
ViewControllerTwo *objViewTwo = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerTwo"];
obj.str2 = str1;
[self.navigationController pushViewController: objViewTwo animated:YES];
}
Do these in ViewControllerTwo.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#", str2);
}
You can save data in an App delegate to access it across view controllers in your application. All you have to do is create a shared instance of an app delegate:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
For Example
If you declare a NSArray object *arrayXYZ, then you can access it in any view controller by appDelegate.arrayXYZ.
NewsViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tbl_View deselectRowAtIndexPath:indexPath animated:YES];
News *newsObj = [newstitleArr objectAtIndex:indexPath.row];
NewsDetailViewController *newsDetailView = [[NewsDetailViewController alloc] initWithNibName:#"NewsDetailViewController" bundle:nil];
newsDetailView.newsHeadlineStr = newsObj.newsHeadline;
[self.navigationController pushViewController:newsDetailView animated:YES];
}
NewsDetailViewController.h
#interface NewsDetailViewController : UIViewController
#property(nonatomic,retain) NSString *newsHeadlineStr;
#end
NewsDetailViewController.m
#synthesize newsHeadlineStr;
Delegation is the only one solution to perform such operations when you are using .xib files. However, all previous answers are for storyboard for .xibs files. You need to use delegation. That's the only solution you can use.
Another solution is use the singleton class pattern. Initialize it once and use it in your entire app.
For SwiftUI
Think of #EnvironmentObject as a smarter, simpler way of using #ObservedObject on lots of views. Rather than creating some data in view A, then passing it to view B, then view C, then view D before finally using it, you can create it in view and put it into the environment so that views B, C, and D will automatically have access to it.
Note: Environment objects must be supplied by an ancestor view – if SwiftUI can’t find an environment object of the correct type you’ll get a crash. This applies for previews too, so be careful.
As an example, here’s an observable object that stores user settings:
class UserSettings: ObservableObject {
#Published var score = 0
}
There are 3 types for passing data one ViewController to another
ViewController.
Programatically
Segue
UserDefaults
Demo Project Link Here - https://github.com/kamanijasmin13/Swift-Pass-data-between-viewcontrollers
Programatically
Segue
UserDefaults
Demo Project Link Here - https://github.com/kamanijasmin13/Swift-Pass-data-between-viewcontrollers
I like the idea of model objects and mock objects based on NSProxy to commit or discard data if what the user selects can be cancelled.
It's easy to pass data around since it's a single object or couple of objects and if you have, let's say, a UINavigationController controller, you can keep the reference to the model inside and all pushed view controllers can access it directly from the navigation controller.
I have seen a lot of people over complicating this using the didSelectRowAtPath method. I am using Core Data in my example.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// This solution is for using Core Data
YourCDEntityName * value = (YourCDEntityName *)[[self fetchedResultsController] objectAtIndexPath: indexPath];
YourSecondViewController * details = [self.storyboard instantiateViewControllerWithIdentifier:#"nameOfYourSecondVC"]; // Make sure in storyboards you give your second VC an identifier
// Make sure you declare your value in the second view controller
details.selectedValue = value;
// Now that you have said to pass value all you need to do is change views
[self.navigationController pushViewController: details animated:YES];
}
Four lines of code inside the method and you are done.

How to use single storyboard uiviewcontroller for multiple subclass

Let say I have a storyboard that contains UINavigationController as initial view controller. Its root view controller is subclass of UITableViewController, which is BasicViewController. It has IBAction which is connected to right navigation button of the navigation bar
From there I would like to use the storyboard as a template for other views without having to create additional storyboards. Say these views will have exactly the same interface but with root view controller of class SpecificViewController1 and SpecificViewController2 which are subclasses of BasicViewController.
Those 2 view controllers would have the same functionality and interface except for the IBAction method.
It would be like the following:
#interface BasicViewController : UITableViewController
#interface SpecificViewController1 : BasicViewController
#interface SpecificViewController2 : BasicViewController
Can I do something like that?
Can I just instantiate the storyboard of BasicViewController but have root view controller to subclass SpecificViewController1 and SpecificViewController2?
Thanks.
great question - but unfortunately only a lame answer. I don't believe that it is currently possible to do what you propose because there are no initializers in UIStoryboard that allow overriding the view controller associated with the storyboard as defined in the object details in the storyboard on initialization. It's at initialization that all the UI elements in the stoaryboard are linked up to their properties in the view controller.
It will by default initialize with the view controller that is specified in the storyboard definition.
If you are trying to gain reuse of UI elements you created in the storyboard, they still must be linked or associated to properties in which ever view controller is using them for them to be able to "tell" the view controller about events.
It's not that much of a big deal copying over a storyboard layout especially if you only need a similar design for 3 views, however if you do, you must make sure that all the previous associations are cleared, or it will get crashes when it tries to communicate to the previous view controller. You will be able to recognize them as KVO error messages in the log output.
A couple of approaches you could take:
store the UI elements in a UIView - in a xib file and instantiate it from your base class and add it as a sub view in the main view, typically self.view. Then you would simply use the storyboard layout with basically blank view controllers holding their place in the storyboard but with the correct view controller sub class assigned to them. Since they would inherit from the base, they would get that view.
create the layout in code and install it from your base view controller. Obviously this approach defeats the purpose of using the storyboard, but may be the way to go in your case. If you have other parts of the app that would benefit from the storyboard approach, it's ok to deviate here and there if appropriate. In this case, like above, you would just use bank view controllers with your subclass assigned and let the base view controller install the UI.
It would be nice if Apple came up with a way to do what you propose, but the issue of having the graphic elements pre-linked with the controller subclass would still be an issue.
have a great New Year!!
be well
The code of line we are looking for is:
object_setClass(AnyObject!, AnyClass!)
In Storyboard -> add UIViewController give it a ParentVC class name.
class ParentVC: UIViewController {
var type: Int?
override func awakeFromNib() {
if type = 0 {
object_setClass(self, ChildVC1.self)
}
if type = 1 {
object_setClass(self, ChildVC2.self)
}
}
override func viewDidLoad() { }
}
class ChildVC1: ParentVC {
override func viewDidLoad() {
super.viewDidLoad()
println(type)
// Console prints out 0
}
}
class ChildVC2: ParentVC {
override func viewDidLoad() {
super.viewDidLoad()
println(type)
// Console prints out 1
}
}
As the accepted answer states, it doesn't look like it is possible to do with storyboards.
My solution is to use Nib's - just like devs used them before storyboards. If you want to have a reusable, subclassable view controller (or even a view), my recommendation is to use Nibs.
SubclassMyViewController *myViewController = [[SubclassMyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
When you connect all your outlets to the "File Owner" in the MyViewController.xib you are NOT specifying what class the Nib should be loaded as, you are just specifying key-value pairs: "this view should be connected to this instance variable name." When calling [SubclassMyViewController alloc] initWithNibName: the initialization process specifies what view controller will be used to "control" the view you created in the nib.
It is possible to have a storyboard instantiate different subclasses of a custom view controller, though it involves a slightly unorthodox technique: overriding the alloc method for the view controller. When the custom view controller is created, the overridden alloc method in fact returns the result of running alloc on the subclass.
I should preface the answer with the proviso that, although I have tested it in various scenarios and received no errors, I can't ensure that it will cope with more complex set ups (but I see no reason why it shouldn't work). Also, I have not submitted any apps using this method, so there is the outside chance that it might be rejected by Apple's review process (though again I see no reason why it should).
For demonstration purposes, I have a subclass of UIViewController called TestViewController, which has a UILabel IBOutlet, and an IBAction. In my storyboard, I have added a view controller and amended its class to TestViewController, and hooked up the IBOutlet to a UILabel and the IBAction to a UIButton. I present the TestViewController by way of a modal segue triggered by a UIButton on the preceding viewController.
To control which class is instantiated, I have added a static variable and associated class methods so get/set the subclass to be used (I guess one could adopt other ways of determining which subclass is to be instantiated):
TestViewController.m:
#import "TestViewController.h"
#interface TestViewController ()
#end
#implementation TestViewController
static NSString *_classForStoryboard;
+(NSString *)classForStoryboard {
return [_classForStoryboard copy];
}
+(void)setClassForStoryBoard:(NSString *)classString {
if ([NSClassFromString(classString) isSubclassOfClass:[self class]]) {
_classForStoryboard = [classString copy];
} else {
NSLog(#"Warning: %# is not a subclass of %#, reverting to base class", classString, NSStringFromClass([self class]));
_classForStoryboard = nil;
}
}
+(instancetype)alloc {
if (_classForStoryboard == nil) {
return [super alloc];
} else {
if (NSClassFromString(_classForStoryboard) != [self class]) {
TestViewController *subclassedVC = [NSClassFromString(_classForStoryboard) alloc];
return subclassedVC;
} else {
return [super alloc];
}
}
}
For my test I have two subclasses of TestViewController: RedTestViewController and GreenTestViewController. The subclasses each have additional properties and each override viewDidLoad to change the background colour of the view and update the text of the UILabel IBOutlet:
RedTestViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.testLabel.text = #"Set by RedTestVC";
}
GreenTestViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.testLabel.text = #"Set by GreenTestVC";
}
On some occasions I might want to instantiate TestViewController itself, on other occasions RedTestViewController or GreenTestViewController. In the preceding view controller, I do this at random as follows:
NSInteger vcIndex = arc4random_uniform(4);
if (vcIndex == 0) {
NSLog(#"Chose TestVC");
[TestViewController setClassForStoryBoard:#"TestViewController"];
} else if (vcIndex == 1) {
NSLog(#"Chose RedVC");
[TestViewController setClassForStoryBoard:#"RedTestViewController"];
} else if (vcIndex == 2) {
NSLog(#"Chose BlueVC");
[TestViewController setClassForStoryBoard:#"BlueTestViewController"];
} else {
NSLog(#"Chose GreenVC");
[TestViewController setClassForStoryBoard:#"GreenTestViewController"];
}
Note that the setClassForStoryBoard method checks to ensure that the class name requested is indeed a subclass of TestViewController, to avoid any mix-ups. The reference above to BlueTestViewController is there to test this functionality.
Basing particularly on nickgzzjr and Jiří Zahálka answers plus comment under the second one from CocoaBob I've prepared short generic method doing exactly what OP needs. You need only to check storyboard name and View Controllers storyboard ID
class func instantiate<T: BasicViewController>(as _: T.Type) -> T? {
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
guard let instance = storyboard.instantiateViewController(withIdentifier: "Identifier") as? BasicViewController else {
return nil
}
object_setClass(instance, T.self)
return instance as? T
}
Optionals are added to avoid force unwrap (swiftlint warnings), but method returns correct objects.
Also: you need to initialize properties existing only in subclass before reading them from casted objects (if subclass has those properties and BasicViewController does not). Those properties won't be initialized automatically and attempt to read them before initialization will lead to crash. Because they are there in effect of casting it's very likely that even weak variables won't be set to nil (will contain garbage).
try this, after instantiateViewControllerWithIdentifier.
- (void)setClass:(Class)c {
object_setClass(self, c);
}
like :
SubViewController *vc = [sb instantiateViewControllerWithIdentifier:#"MainViewController"];
[vc setClass:[SubViewController class]];
Although it's not strictly a subclass, you can:
option-drag the base class view controller in the Document Outline to make a copy
Move the new view controller copy to a separate place on the storyboard
Change Class to the subclass view controller in the Identity Inspector
Here's an example from a Bloc tutorial I wrote, subclassing ViewController with WhiskeyViewController:
This allows you to create subclasses of view controller subclasses in the storyboard. You can then use instantiateViewControllerWithIdentifier: to create specific subclasses.
This approach is a bit inflexible: later modifications within the storyboard to the base class controller don't propagate to the subclass. If you have a lot of subclasses you may be better off with one of the other solutions, but this will do in a pinch.
Objc_setclass method doesn't create an instance of childvc. But while popping out of childvc, deinit of childvc is being call. Since there is no memory allocated separetely for childvc, app crashes. Basecontroller has an instance , whereas child vc doesn't have.
If you are not too reliant on storyboards, you can create a separate .xib file for the controller.
Set the appropriate File's Owner and outlets to the MainViewController and override init(nibName:bundle:) in the Main VC so that its children can access the same Nib and its outlets.
Your code should look like this:
class MainViewController: UIViewController {
#IBOutlet weak var button: UIButton!
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: "MainViewController", bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
button.tintColor = .red
}
}
And your Child VC will be able to reuse its parent's nib:
class ChildViewController: MainViewController {
override func viewDidLoad() {
super.viewDidLoad()
button.tintColor = .blue
}
}
There is a simple, obvious, everyday solution.
Simply put the existing storyboard/controller inside the new storyobard/controller. I.E. as a container view.
This is the exactly analogous concept to "subclassing", for, view controllers.
Everything works exactly as in a subclass.
Just as you commonly put a view subview inside another view, naturally you commonly put a view controller inside another view controller.
How else can could you do it?
It's a basic part of iOS, as simple as the concept "subview".
It's this easy ...
/*
Search screen is just a modification of our List screen.
*/
import UIKit
class Search: UIViewController {
var list: List!
override func viewDidLoad() {
super.viewDidLoad()
list = (_sb("List") as! List
addChild(list)
view.addSubview(list.view)
list.view.bindEdgesToSuperview()
list.didMove(toParent: self)
}
}
You now obviously have list to do whatever you want with
list.mode = .blah
list.tableview.reloadData()
list.heading = 'Search!'
list.searchBar.isHidden = false
etc etc.
Container views are "just like" subclassing in the same way that "subviews" are "just like" subclassing.
Of course obviously, you can't "sublcass a layout" - what would that even mean?
("Subclassing" relates to OO software and has no connection to "layouts".)
Obviously when you want to re-use a view, you just subview it inside another view.
When you want to re-use a controller layout, you just container view it inside another controller.
This is like the most basic mechanism of iOS!!
Note - for years now it's been trivial to dynamically load another view controller as a container view. Explained in the last section: https://stackoverflow.com/a/23403979/294884
Note - "_sb" is just an obvious macro we use to save typing,
func _sb(_ s: String)->UIViewController {
// by convention, for a screen "SomeScreen.storyboard" the
// storyboardID must be SomeScreenID
return UIStoryboard(name: s, bundle: nil)
.instantiateViewController(withIdentifier: s + "ID")
}
Thanks for #Jiří Zahálka's inspiring answer, I replied my solution 4 years ago here, but #Sayka suggested me to post it as an answer, so here it is.
In my projects, normally, if I'm using Storyboard for a UIViewController subclass, I always prepare a static method called instantiate() in that subclass, to create an instance from Storyboard easily. So for solve OP's question, if we want to share the same Storyboard for different subclasses, we can simply setClass() to that instance before returning it.
class func instantiate() -> SubClass {
let instance = (UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SuperClass") as? SuperClass)!
object_setClass(instance, SubClass.self)
return (instance as? SubClass)!
}
Here is a Swift solution which does not rely on Objective-C class swapping hacks.
It uses instantiateViewController(identifier:creator:) (iOS 13+).
I assume you have the view controller in a storyboard, with identifier template. The class assigned to the view controller in the storyboard should be the superclass:
let storyboard = UIStoryboard(name: "main", bundle: nil)
let viewController = storyboard.instantiateViewController(identifier: "template") { coder in
// The coder provides access to the storyboard data.
// We can now init the preferred UIViewController subclass.
if useSubclass {
return SpecialViewController(coder: coder)
} else {
return BaseViewController(coder: coder)
}
}
Here is the documentation
Probably most flexible way is to use reusable views.
(Create a View in separate XIB file or Container view and add it to each subclass view controller scene in storyboard)
Taking answers from here and there, I came up with this neat solution.
Create a parent view controller with this function.
class ParentViewController: UIViewController {
func convert<T: ParentViewController>(to _: T.Type) {
object_setClass(self, T.self)
}
}
This allows the compiler to ensure that the child view controller inherits from the parent view controller.
Then whenever you want to segue to this controller using a sub class you can do:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let parentViewController = segue.destination as? ParentViewController {
ParentViewController.convert(to: ChildViewController.self)
}
}
The cool part is that you can add a storyboard reference to itself, and then keep calling the "next" child view controller.
Cocoabob's comment from Jiří Zahálka answer helped me to get this solution and it worked well.
func openChildA() {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let parentController = storyboard
.instantiateViewController(withIdentifier: "ParentStoryboardID")
as! ParentClass;
object_setClass(parentController, ChildA.self)
self.present(parentController, animated: true, completion: nil);
}
It is plain simple. Just define the BaseViewController in a xib and then use it like this:
let baseVC: BaseViewController = BaseViewController(nibName: "BaseViewController", bundle: nil)
let subclassVC: ChildViewController = ChildViewController(nibName: "BaseViewController", bundle: nil)
To make is simple you can extract the identifier to a field and the loading to a method like:
public static var baseNibIdentifier: String {
return "BaseViewController"
}
public static func loadFromBaseNib<T>() -> T where T : UIViewController {
return T(nibName: self.baseNibIdentifier, bundle: nil)
}
Then you can use it like this:
let baseVC: BaseViewController = BaseViewController.loadFromBaseNib()
let subclassVC: ChildViewController = ChildViewController.loadFromBaseNib()

prepareForSegue is not called after performSegue:withIdentifier: with popover style

I have a universal app, where I am sharing the same controller for a IPad and IPhone storyboard.
I have put a UILongPressGestureRecognizer on a UITableView, that when a cell is pressed on iPhone it calls an action that perform a segue:
-(IBAction)showDetail:(id)sender {
UILongPressGestureRecognizer *gesture = (UILongPressGestureRecognizer*)sender;
if (gesture.state == UIGestureRecognizerStateBegan) {
CGPoint p = [gesture locationInView:self.theTableView];
NSIndexPath *indexPath = [self.theTableView indexPathForRowAtPoint:p];
if (indexPath != nil) {
[self performSegueWithIdentifier:SEGUE_DETAIL sender:indexPath];
}
}
}
the segue is a detail view performed as a 'push'. The first thing you should notice is that the sender is an NSIndexPath, is the only way I found for passing the selected cell. Maybe there's a better solution.
Everything works fine, in a sense that the segue is performed, and before the prepareForSegue is called too.
However it happens that on iPad, I have changed the segue identifier to Popover.
Now things are working in part, the segue is performed, but prepareForSegue is not called and therefore the destination view controller is not set up as it should be.
What am I doing wrong ?
What I have discovered so far, is that with any segue identifier that is not popover these are the invocations made by iOS:
prepareForSegue (on source controller)
viewDidLoad (on destination controller)
while in popover segue the invocation order is:
viewDidLoad (on destination controller)
prepareForSegue (on source controller)
just because I put all my logic in viewDidLoad, the controller was not properly initialized, and a crash happened. So this is not exactly true that prepareForSegue is not called, the truth is that I was getting an exception, and I wrongly mistaken as prepareForSegue not getting called.
I couldn't put everything in viewWillAppear because a call to CoreData had to be made and I didn't want to check if entities were ok each time the view display.
How did I solve this ? I created another method in destination controller
-(void)prepareViewController {
// initialization logic...
}
and changing the prepareForSegue method in source controller itself:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MyViewController *mvc = (MyViewController*)[segue destinationViewController];
// passing variable
// with segue style other than popover this called first than viewDidLoad
mvc.myProp1=#"prop1";
mvc.myProp2=#"prop2";
// viewWillAppear is not yet called
// so by sending message to controller
// the view is initialized
[mvc prepareViewController];
}
don't know if this is expected behavior with popover, anyway now things are working.
I've noticed that the boiler plate code for Xcode's Master-Detail template (iPhone) uses the following pattern for configuring the detail VC's view:
the detail VC's setters (for properties) are overwritten in order to invoke the configureView method (configureView would update all your controls in the view, e.g., labels, etc.)
the detail VC's viewDidLoad method also invokes the configureView method
I did not follow this pattern the other day when I was trying to re-use a detail VC in my movie app, and this gave me trouble.
I don't have much experience with popovers; however, if the pattern above is used with a detail VC that is displayed inside a popover, then wouldn't the detail VC's view get configured when you set the detail VC's properties from within the prepareForSegue method?