Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 616 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing data between view controllers

#11
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][1]*

It worked fine and allowed me to pass a string and an array forward and back between my views.

[1]:

[To see links please register here]


Reply

#12
**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.



Reply

#13
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. ;)



Reply

#14
Use notification center to pass data from one view to another.

The observer listener pattern works the best. The other workaround can be create the same objects in both classes.

Create a class 2 object in class one. Access the data objects to be passed, set them, and then push the view controller.
Reply

#15
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.


Reply

#16
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.
Reply

#17
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.


Reply

#18
**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:

1. We need to create a string object in the *SecondViewController.h* file

NSString *strValue;

2. Need to declare a property as the below declaration in the *.h* file

@property (strong, nonatomic) NSString *strSecondValue;

3. 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;

4. 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 ];

Reply

#19
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];
}




Reply

#20
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];



Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through