How to retrieve view from controller of another view? - sapui5

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

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 count element of one controller into another

i've a question to ask for my mvc project...
i have two controllers: Giurisprudenza e GiurisprudenzaNode, these controllers implement an index with the list of elements and a crud system to add/edit/remove elements. In the Girusiprudenza view page every element has a button that return you into the relative GiurisprudenzaNode page, with the list of elements contained into the primary element.
I would like to insert a label next to every element of the list in Giurisprudenza that say how many elements there are into that single primary element, but i don't know how to do that count... Anybody can help me?
for this you dont need two controllers or two different view .
For this you need to follow below steps.
Create a viewModel (Customize mix of both models)
add custom property in you this viewmodel for counts
create new view for this ViewModel class .
Let me know if you need code snippet for this.
View Model from MSDN

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.

Passing data from one view controller to another without using Segue

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

Eclipse RCP - Programmatically setting a view to not be closeable

I'm trying to create an RCP view that is not closeable. I need a way to set this property programmatically because I'm creating views with secondary IDs in in the code. I can't do it through the extension editor dialogs because of this.
Is there a way to remove the x from a view programmatically?
I was finally able to figure this out.
In your perspective's createInitialLayout() function you can get the layout of the view and set its closeable property:
IViewLayout vLayout = layout.getViewLayout(View.ID);
vLayout.setCloseable(false);
This will work for views with secondary ids too. The code would be exactly the same in that case, because it will apply the closeable property to all secondary views that share the same primary id.
I've found that the following won't work:
IViewLayout vLayout = layout.getViewLayout(View.ID + ":1");
vLayout.setCloseable(false);
So you can't make individual views closeable based on their secondary ids. Either the whole group is or isn't.
You can do it easily.
Just set the closable property of view to False.
IViewLayout layout= layout.getViewLayout(View.ID);
layout.setCloseable(false);