2
votes

I am new to Titanium.

I have taken 3 view and want to hide show that view on button click for iPhone app.

Any idea how to achieve this?

2
Can you be more specific? What you actually want to do, did you tried any code etc.. - Anand

2 Answers

1
votes

You can hide / show a view very easily, heres a self contained example:

var win = Ti.UI.createWindow();
var view = Ti.UI.createView({
   width : 100,
   height : 100,
   backgroundColor : 'red' 
});

var redView = Ti.UI.createView({
    title : 'Hide / Show Red View',
    bottom : 0,
    width : 200,
    height : 35
});
var visible = true;
button.addEventListener('click', function(e) {
   if(visible)  {
       redView.hide();
   } else {
       redView.show();
   }
   visible = !visible;
});

win.add(redView);
win.add(button);
win.open();
1
votes

While the other answer is certainly useful, two other (slightly) different methods for doing the same thing, just in case Titanium flips out when you use show() and hide().

//Method 1, using part of Josiah Hester's code snippet
var visible = true;
button.addEventListener('click', function(e) {
   if(visible)  {
       redView.setVisible(false); //This is the exact same thing as hide() method
   } else {
       redView.setVisible(true); //This is the exact same thing as show() method
   }
   visible = !visible;
});

You can set opacity to 0 and even if the view's visible property is set to true, it will still be invisible, due to a completely nonexistent level of opacity. This is useful if you want something to be visible, but not clickable (by placing a view behind a view with an opacity of zero.

//Method 2, same code section
var opacity = 0;
button.addEventListener('click', function(e) {
   if(opacity)  {
       redView.setOpacity(0); //This is the NOT the same thing as hide() method
   } else {
       redView.setOpacity(1); //This is the NOT thesame thing as show() method
   }
   opacity = Math.abs(opacity - 1);
});