Passing data from one view controller to another without using Segue - iphone

I have a UITableView (let’s say class “Parent” which is in view "screen 1"). When I click on one of the Parent’s cell's, I’m displaying another UITableView (let’s call it class “child” in view “Screen2”). Parent and child are connected through “segue” and I’m able to pass the data using “segue”.
For example, parent is having cell – “Cell1” and on touch “Cell1”, I’m getting “Cell11”,”Cell12”,”Cell13” [this is “screen2, table view object”].
Now, there are some descriptions associated with “Cell11” (Once I touched “Cell11”) and i'd like it to display Cell11’s description in another view controller (“screen3”). Here, to pass information between “screen2” to “screen3” I'd rather not to use a “Segue”.
How can I do this?

There are two ways in which you can present a new screen.
1) with the interface builder, here you use a segue to pass something onto the new screen.
2) manually instantiating it, after it is instantiated you can just present it.
Considering you do not want to use method 1, you can use method two like this:
ScreenBClass *screenB = [self.storyboard instantiateViewControllerWithIdentifier:#"screenB"];
screenB.objectPointerInOtherClass = self.objectIWantToPass;
[self screenB animated:YES];

Related

Accessing function and model in one controller from another view's controller UI5

I have 2 view A and B with their respective controllers and models defined inside. Due to some reason I have to move 2 elements from A to B view. One of the element is a button link with a press event. I moved the elements to another view but when I click on the link, it does not trigger press. Which I understand is because B 's controller does not have function for that. If I write the same function in B's controller I get an error saying the model's setProperty could not be set as not found. Because that model is in scope in A's controller only. How do I access it?
Press event of link:
pressEvent: function(oEvent) {
this.getModel("stock").setProperty("/Links/Visible", true);
var stockroomsarr = this.getModel("stockRms").getProperty("/Stockrooms");
if (stockrooms !== null && stockrooms.length > 0) {
this.getModel("stock").setProperty("/Text/Visible", false);
}
this.getModel("stock").refresh(false);
},
Regarding your Model: I strongly advice not to have own models for each controllers. Instead you should define and set them in the init of your component.js. They then are accessable in the view controller by calling
this.getOwnerComponent().getModel("urModel")
Another variant would be to create a baseController. In that you can define functions which are to be used by multiple controllers as well as your models. All other controllers extend that baseController, so the models are available in all of them via:
this.getView().getModel("urModel")
Regarding the function you may use the Event Bus. In short it allows you to create events that are triggered in controller A and are listned to in controller B. Read up here for detailed information on how to use it:
https://blogs.sap.com/2015/10/25/openui5-sapui5-communication-between-controllers-using-publish-and-subscribe-from-eventbus/

How to retrieve view from controller of another view?

I am just wondering how can I get one of the UI5 views in an application.
I know there is a method:
sap.ui.jsview(); // in case the view is written in JavaScript
But the problem with this method is: if you assign ID for any of the controls and you have already inflated this view, you get an error.
So I want to know how to check if the view already exists and then if yes return that existing view, otherwise create the view with the corresponding API such as the above one.
I also know in the control for view I can go
this.getView();
But as I said, how to get this view from another view?
I am not quite understanding your question
With managed object id's are unique, so if you try and create the same view twice you will get an error.
when you create your view the easiest way to access it is via an Id
sap.ui.jsview("view1",'testapp.view.view1');
sap.ui.getCore().byId('view1');
NB. views should not talk to anyone other than their controller A terrific Model View Controller (MVC) diagram
sap.ui.getCore().byId(<id_of_the_view>)
Where <id_of_the_view> can be obtained in the following way:
suppose that the corresponding controller of <id_of_the_view> is "controllerA.js",
then you can console.log, inside controllerA.js,
console.log(this.getView())
This will print you an object which contains the id of such view. This will be <id_of_the_view>
I think here is one solution.
Make a global variable;
Use it to create element.
In First View:
var mytextField ;(use it as global)
mytextField = new sap.ui.commons.TextField('textfieldId');
In Second View:
var myValue = mytextField .getValue();
~Mansi Rao

Xcode: he first view doesn't see changes made by the second one

I'm a very newbie in the obj-c programming and have some troubles trying to change values between two views. I'm using Xcode 4.5 and storyboards and I've some problems with passing a changed value from the second view to the first one.
Here's my 2 very simple views (posting the link as I'm a new user and can't post images):
https://www.dropbox.com/s/q4o2bblu1p57zod/img.png
These views are assigned to the same class (ViewController) and the code I'm using to change the 2 labels is:
-(IBAction)setLabel:(id)sender
{
if (myTextField.text.length != 0) {
myLabel1.text = myTextField.text;
myLabel2.text = myTextField.text;
}
}
The problem is that Label1 changes correctly its text, but there's nothing to do with Label2! It doesn't want to change...
I think I'm trying to do something that can be made in others ways...Can you please tell me if it's correct?
You need to use Protocol-Delegate approach to update content in First view.
I suggest you to visit this sample link.
Your two view controllers may be of the same class but they are going to be different objects at runtime. You have a segue between them and when that executes a new instance will be created. Since the 'label2' of the second instance is not displayed on its screen, your assignment doesn't produce a visible change.

How do you pass data from one text field to another when using Storyboards in iOS5?

I am using storyboards for the 1st time, and I cant figure out what I am doing wrong here ... I have a button that transitions from one view controller to another using StoryBoards (the 2nd view is presented modally).
I am trying to use the "prepare for segue" in order to pass the value of a text field from view 1 to view 2, but it is not working. Can somebody tell me what I have wrong here ... ?
View 1:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"saveGame"]) {
statsViewController *svc = (statsViewController *)[segue destinationViewController];
[svc setStatsTextField:gameTextField];
}
}
If there is other code I can post to clarify please let me know.
(for the record there are no errors, the text field on view 2 just doesn't update.)
You cannot just assign a text field in one view controller to a property in another one. This achieves nothing for the text field that is actually in the second view controller's view. Instead, you have to assign a value to the text field's text property. (And ideally, this value should not come directly from another text field because you shouldn't use views to store your app's data. Whenever a text field updates, you should store the updated value in a variable in your view controller or model.)
Also, the statsTextField does not yet exist at the time this code is executed because the destination view controller's view is not yet loaded. You should declare a separate string property in statsViewController (class names should begin with a capital letter btw) and then assign the text field's value in viewDidLoad.
The text field is probably nil at that point since the view hasn't been loaded. You can force this (no problem doing so since its about to happen anyway!) by wrapping your code in an if statement:
if (svc.view)
svc.textfield.text = #"Hello";
Accessing the view property forces the view controller to load the view, if it is not already present.
I notice you seem to be passing a whole textfield object instead of a string to the text property - that doesn't seem like a good idea. It should be more like my example above.

Navigation object

I have a navigation based application. when I click on a table's row next view appears and back button (manually created )appears. Now i want to know how can I get the object of previous view in current view so that I can change one of the label's text of previous view using previous view's object ?
Thanks.
You can pass the value before pushing the second view to the necessary variable
secondViewController.variable = firstViewController.variable;
Then push the secondViewController
Update:
Use a Bool variable willBePoppedBack as class variable and set it as NO initially.
- (void)viewWillAppear {
if(willBePoppedBack)
{
// your label text after pressing back button
}
else {
// your label's default text
}
}
You should set the variable willBePoppedBack to YES when you push to the next view.
If you want to do this you need to make a object of previous view here and then by . operator you can access first page label and write some thing on it.
FirstView *obj=[[[FirstView allo] initWithNibName:#"FirstView"] autorelease];
obj.label.text=#"Text";
Edit:
you need to make a NSString property in appDelegate class then make the object for app delegate and set the value of this string which you want on label
now pop the view and in viewWillAppear use this string to write on the label.
It must work.
Initialize the second view by passing the tableviews object, check the below link, with the similar approach you can achieve your requirement.
navigation based