Scrollable view problem in adding buttons

You must Login before you can answer or comment on any questions.

Hi, I am using titanium sdk version 2.1.0.GA IOS simulator host operating system is MacOS. Titanium Studio, build: 2.1.0.201206251749. i am trying to add a button to all the views that are in scrollableview. The button does not appear. please help .Below is my code.Thanks in advance.

var startedScrollView = Titanium.UI.createScrollableView({
    showPagingControl : true,
    backgroundImage : 'ui/common/images/background.png',
    scrollingEnabled : false,
    exitOnClose : true
});
 
var startedView1 = Ti.UI.createView({
    top : 0,
    width : Ti.Platform.displayCaps.platformWidth,
    height : '100%',
});
var startedView2 = Ti.UI.createView({
    top : 0,
    width : Ti.Platform.displayCaps.platformWidth,
    height : '100%',
});
 
 
var getStartedButton = Ti.UI.createButton({
    title : 'Get Started',
    color : '#000000',
    text  : 'GetStarted',
    width :100,
    height: 40,
    bottom: 0,
    align : 'center'
});
getStartedButton.addEventListener('click', function(e) {
    sleep(2000);
    clearInterval(intervalTimer);
    Ti.include('ui/common/activibeViews/Login.js');
    firstwin.close();
})
 
startedView1.add(getStartedButton);
startedView2.add(getStartedButton);
infoViewArray.push(startedView1);
infoViewArray.push(startedView2);
startedScrollView.views = infoViewArray;
win.add(startedScrollView);
win.open();

3 Answers

Accepted Answer

Cannot add the same button object to multiple parents.

You could build a Constructor function and add a 'new' object based upon the Constructor to each view.

— answered 10 months ago by Stephen Feather
answer permalink
5 Comments
  • kind of new to titanium..can you give me some links or examples where this concept is explained..will really be helpful..

    — commented 10 months ago by Shashank Dass

  • This is described in the docs under best practices. Check here: http://docs.appcelerator.com/titanium/2.1/index.html#!/guide/Building_Reusable_Factories

    Please pay attention to the Overview section and follow the links accordingly for more advanced info.

    — commented 10 months ago by Anthony Decena

  • Yup this worked..i created that button for all the view..thank you so much guys for help..

    — commented 10 months ago by Shashank Dass

  • Show 2 more comments

The button should actually appear on the second screen. The reason is because you can't reuse elements like that. You need to create a factory method that creates a new button for each view.

— answered 10 months ago by Anthony Decena
answer permalink
3 Comments
  • it is not appearing on any of them

    — commented 10 months ago by Shashank Dass

  • I altered your code to run in a single app.js file and the button showed up on the second screen. Here is the code so you can see it yourself.

    var win = Ti.UI.createWindow({
        backgroundColor:'#fff'
    });
     
    var startedScrollView = Titanium.UI.createScrollableView({
        showPagingControl : true,
        //backgroundImage : 'ui/common/images/background.png',
        scrollingEnabled : false,
        exitOnClose : true
    });
     
    var startedView1 = Ti.UI.createView({
        top : 0,
        width : Ti.Platform.displayCaps.platformWidth,
        height : '100%',
    });
    var startedView2 = Ti.UI.createView({
        top : 0,
        width : Ti.Platform.displayCaps.platformWidth,
        height : '100%',
    });
     
     
    var getStartedButton = Ti.UI.createButton({
        title : 'Get Started',
        color : '#000000',
        text  : 'GetStarted',
        width :100,
        height: 40,
        bottom: 0,
        align : 'center'
    });
    getStartedButton.addEventListener('click', function(e) {
        alert('Close Shit');
    })
     
    startedView1.add(getStartedButton);
    startedView2.add(getStartedButton);
    infoViewArray = [];
    infoViewArray.push(startedView1);
    infoViewArray.push(startedView2);
    startedScrollView.views = infoViewArray;
    win.add(startedScrollView);
     
     
    win.open();
    I had to make a few changes in order to make it work in context. I didn't have the backgroundImage for the ScrollableView and I removed everything in the button event listener replacing it with an alert. Finally, I had to define the infoViewArray.

    If this doesn't work for you either, please share your dev environment. We need platform version, sdk version, and os and version.

    — commented 10 months ago by Anthony Decena

  • it doesnt work.. my os version is 10.7.4..rest i have already shared,,but i think we are diverting from the question altogether...even if i am able to add it to the second view than also it is a problem.. i need to add to all the views in scrollable view..Thanks for the help

    — commented 10 months ago by Shashank Dass

Your Answer

Think you can help? Login to answer this question!