Titanium EventListener Not Working - iphone

I am new for Titanium and creating my dummy app for iPhone.
I was using this code from a tutorial.
But am stuck in 'itemSelected' eventListener, looks like not working here.
I have tried by adding button, label and their click events even they are not working.
I am not getting my mistake here.
So please advice.
Thanks in advance.
My ApplicationWindow.js is..
//Application Window Component Constructor
function ApplicationWindow() {
//declare module dependencies
var MasterView = require('ui/listView_common/MasterView'),
DetailView = require('ui/listView_common/DetailView');
//create object instance
var self = Ti.UI.createWindow({
backgroundColor:'#fff'
});
//construct UI
var masterView = new MasterView(),
detailView = new DetailView();
//create master view container
var masterContainerWindow = Ti.UI.createWindow({title:'List View'});
masterContainerWindow.add(masterView);
//create detail view container
var detailContainerWindow = Ti.UI.createWindow({left:100,title:'Detail View'});
detailContainerWindow.add(detailView);
//create iOS specific NavGroup UI
var navGroup = Ti.UI.iPhone.createNavigationGroup({
window:masterContainerWindow
});
self.add(navGroup);
//add behavior for master view
masterView.addEventListener('itemSelected', function(e) {
alert("Alert");
navGroup.open(detailContainerWindow);
detailView.showArticle(e.link);
});
function refreshData() {
var file = Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory+'jsonFiles/data.json');
var data = file.read().text;
var json = JSON.parse(data);
masterView.refreshDataTable(json);
}
// load data
refreshData();
return self;
};
module.exports = ApplicationWindow;
and my 'MasterView.js' is..
var createRow = function(item) {
var tablerow = Ti.UI.createTableViewRow({
height: 90,
link: item.link,
className: 'itemRow',
hasChild: true
});
var imageview = Ti.UI.createImageView({
image: item.image,
height: 55,
width: 68,
left: 5,
top: 3
});
var titleview = Ti.UI.createLabel({
text: item.title,
color: '#000',
font: {
fontSize: 16
},
left: 83,
right: 5,
top:5,
width:300
});
var dateview = Ti.UI.createLabel({
text: item.pubDate,
textAlign: 'center',
color: '#444',
font: {
fontSize: 12
},
height: 'auto',
width: 68,
left: 5,
top: 60
});
var nameview = Ti.UI.createLabel({
text: item.firstName +" " + item.lastName,
color: '#000',
font: {
fontSize: 14
},
left: 83,
right: 5,
top:30
});
var descriptionview = Ti.UI.createLabel({
text: item.description,
color: '#000',
font: {
fontSize: 12
},
left: 83,
top:50
});
tablerow.add(imageview);
tablerow.add(dateview);
tablerow.add(titleview);
tablerow.add(nameview);
tablerow.add(descriptionview);
return tablerow;
};
//Master View Component Constructor
function MasterView() {
var self = Ti.UI.createView({
backgroundColor:'#fff'
});
var table = Ti.UI.createTableView();
self.add(table);
table.addEventListener('click', function(e) {
self.fireEvent('itemSelected', { link: e.row.link });
});
self.refreshDataTable = function(data) {
if (Object.prototype.toString.apply(data) === '[object Array]') {
var rows = [];
for (var i = 0; i < data.length; i++) {
rows.push(createRow(data[i]));
}
table.setData(rows);
}
};
return self;
}
module.exports = MasterView;
DetailView.js
//Detail View Component Constructor
function DetailView() {
var self = Ti.UI.createView();
var webview = Ti.UI.createWebView();
self.add(webview);
self.showArticle = function(url) {
webview.url = url;
};
webview.addEventListener('load', function(e) {
self.fireEvent('articleLoaded');
});
return self;
}
module.exports = DetailView;

itemSelected is not a event type of window. That's why it's not working.
Try the click event and get apiName and perform your work based on the apiName.
Example:
windowNAme.addEventListener('click',function(e){
//when we click on button then we get id from this
getId=e.source.id;
if(getId=="mybutton")
{
perform your action here;
}
})

Try to use Titanium.App.addEventListener in your code as follows
//add behavior for master view
Ti.App.addEventListener('itemSelected', function(e) {
alert("Alert");
navGroup.open(detailContainerWindow);
detailView.showArticle(e.link);
});
and fire the event like
table.addEventListener('click', function(e) {
Ti.App.fireEvent('itemSelected', { link: e.row.link });
});
This will do the trick
You may also refer what is fireevent how do i use it

Related

how to add flyout text to flyout in button programatically

Im a generating this button and i would like to set text in the flyout
var button = new Button();
button.Height = height;
button.Width = width;
button.Margin = new Thickness(left, top, 0, 0);
button.BorderBrush = new SolidColorBrush(Windows.UI.Colors.Red);
button.BorderThickness = new Thickness(2);
button.Background = new SolidColorBrush(Windows.UI.Colors.Transparent);
button.Flyout = new Flyout();
how do i set content of button.Flyout?
how do i set content of button.Flyout?
You could pass button instance to the follow method.
public void AddFlyoutToBtn(Button TargetBtn)
{
TargetBtn.Flyout = new Flyout()
{
Content = new StackPanel
{
Children =
{
new TextBlock
{
Text = "All items will be removed. Do you want to continue?",
Margin = new Thickness(10, 10, 0, 0)
},
new Button
{
Content = "Yes, empty my cart"
}
}
}
};
}

Xamarin.iOS Draw a Border for bottom of the UITextField / ViewDidAppear is too late

I'm coding in Xamarin.iOs and I'd like to add a border for bottom of the UITextField.
So it's simple, I've googled it and (I personalized it to Xamarin.ios and) I have this code from this link.
UITextField _textField = new UITextField();
_textField.Placeholder = placeHolder;
_textField.AutocapitalizationType = autocap.Value;
_textField.AutocorrectionType = uitextAutocorrect.Value;
_textField.BorderStyle = borderstyle.Value;
_textField.SecureTextEntry = secureTextEntry.Value;
_textField.KeyboardType = uiKeyboardType.Value;
return _textField;
As you know I cannot put this part of code in constructor of my ViewController and also in ViewWillAppear. So I have to put it in ViewDidAppear, BUT it's too late, it means that when I run ViewController, it show the textField without borders, and just after a few milliseconds, the border will appear which is toooo late for me.
Any idea for this display problem?
EDIT:
So I ask my question in much more detail :
here is my complete code:
public class ChangePasswordViewController: UIViewController
{
//private MainViewModel _viewModel;
private UILabel _oldPasswordLabel;
private UITextField _oldPasswordTextField;
private UILabel _newPasswordLabel;
private UITextField _newPasswordTextField;
private UILabel _confirmPasswordLabel;
private UITextField _confirmPasswordTextField;
private User currentUser;
private UIButton _saveNewPassworBtn;
public ChangePasswordViewController(User user)
{
currentUser = user;
}
private void _saveNewPassworBtn_TouchUpInside(object sender, EventArgs e)
{
//do sth
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
//_viewModel = new MainViewModel();
Title = Texts.ChangePassword;
View.BackgroundColor = UIColor.White;
_oldPasswordLabel = InitUILabel(Texts.OldPasswordTxt, alignment: UITextAlignment.Right);
_oldPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_oldPasswordLabel.AdjustsFontSizeToFitWidth = true;
_oldPasswordTextField = InitUITextField(Texts.OldPassword, secureTextEntry: true);
this._oldPasswordTextField.ShouldReturn += (textField) => {
_newPasswordTextField.BecomeFirstResponder();
return true;
};
_oldPasswordTextField.BorderStyle = UITextBorderStyle.None;
_newPasswordLabel = InitUILabel(Texts.NewPasswordTxt, alignment: UITextAlignment.Right);
_newPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_newPasswordTextField = InitUITextField(Texts.NewPassword, secureTextEntry: true);
this._newPasswordTextField.ShouldReturn += (textField) => {
_confirmPasswordTextField.BecomeFirstResponder();
return true;
};
_newPasswordTextField.BorderStyle = UITextBorderStyle.None;
_confirmPasswordLabel = InitUILabel(Texts.ConfirmPasswordTxt, alignment: UITextAlignment.Right);
_confirmPasswordLabel.Font = UIFont.BoldSystemFontOfSize(14);
_confirmPasswordTextField = InitUITextField(Texts.NewPassword, secureTextEntry: true);
this._confirmPasswordTextField.ShouldReturn += (textField) => {
textField.ResignFirstResponder();
return true;
};
_confirmPasswordTextField.BorderStyle = UITextBorderStyle.None;
_saveNewPassworBtn = InitUIButton(Texts.Save, _saveNewPassworBtn_TouchUpInside, Colors.MainColor, UIColor.White);
View.AddSubviews(_oldPasswordLabel, _oldPasswordTextField, _newPasswordLabel, _newPasswordTextField,
_saveNewPassworBtn, _confirmPasswordLabel, _confirmPasswordTextField);
//
//constraints
var hMargin = 10;
var hMiniMargin = 5;
var vMargin = 10;
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
View.AddConstraints(
_oldPasswordLabel.AtTopOf(View, 100),
_oldPasswordLabel.AtLeftOf(View, hMargin),
_oldPasswordTextField.Below(_oldPasswordLabel, hMiniMargin),
_oldPasswordTextField.AtLeftOf(View, hMargin),
_oldPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_newPasswordLabel.Below(_oldPasswordTextField, vMargin),
_newPasswordLabel.AtLeftOf(View, hMargin),
_newPasswordTextField.Below(_newPasswordLabel, hMiniMargin),
_newPasswordTextField.AtLeftOf(View, hMargin),
_newPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_confirmPasswordLabel.Below(_newPasswordTextField, vMargin),
_confirmPasswordLabel.AtLeftOf(View, hMargin),
_confirmPasswordTextField.Below(_confirmPasswordLabel, hMiniMargin),
_confirmPasswordTextField.AtLeftOf(View, hMargin),
_confirmPasswordTextField.WithSameWidth(View).Minus(hMargin * 2),
_saveNewPassworBtn.Below(_confirmPasswordTextField, vMargin * 3),
_saveNewPassworBtn.WithSameWidth(View).Minus(20),
_saveNewPassworBtn.AtLeftOf(View, 10)
);
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.NavigationItem.SetLeftBarButtonItem(new UIBarButtonItem(Texts.Cancel,UIBarButtonItemStyle.Plain, (sender, args) =>
{
this.NavigationController.SetNavigationBarHidden(false, false);
this.NavigationController.PushViewController(new ProfileViewController(), false);
}), true);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
}
In these lines:
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);
I put the border for my textfields BUT! If I put these line in ViewDidLoad or ViewWillAppear or ViewDidLayoutSubviews, it doesn't show the changes and if I put them in ViewDidAppear, it's shows perfectly with some milliseconds delay (after showing the content of my page!!!). Do you have any solution for me?
It caused by using Cirrious.FluentLayouts.
When we set the Constraints in ViewDidLoad, those control will not finish rendering before ViewDidAppear .
Print the frame of those textfields in ViewDidLoad, ViewWillAppear ,ViewDidAppear , you can find that only in ViewDidAppear the frame is not equal (0,0,0,0).
To solve the problem, you can add View.LayoutIfNeeded in ViewDidLoad to force update the view.
View.AddConstraints(
//xxx
);
View.LayoutIfNeeded(); //Add this
_newPasswordTextField = SetBottomBorderLine(_newPasswordTextField);
_confirmPasswordTextField = SetBottomBorderLine(_confirmPasswordTextField);
_oldPasswordTextField = SetBottomBorderLine(_oldPasswordTextField);

appcelerator titanium iPhone twitter timeline resize

I'm working in appcelerator and I have a tab which is the profile. On the top half there is the profile picture, the name, and other misc items, then on the bottom half is a twitter timeline. When i started putting on the twitter timeline code, it takes up the whole screen. Is there a way to reduce the timeline to only take up a portion of the screen to be able to see the other items?
This is my twitter timeline code:
var twitterUserName = "foundationsix";
var httpClient = Ti.Network.createHTTPClient();
httpClient.timeout = 10000;
httpClient.open("GET","http://api.twitter.com/1/statuses/" +
"user_timeline.json?count=10&screen_name=" + twitterUserName);
var twitterData = [];
httpClient.onload = function() {
try {
var tweets = JSON.parse(this.responseText);
for (var i=0; i < tweets.length; i++) {
var tweetText = tweets[i].text;
var user = tweets[i].user.screen_name;
var avatar = tweets[i].user.profile_image_url;
var created_at = tweets[i].created_at;
var row = Ti.UI.createTableViewRow({hasChild:true,
height:'auto'});
var postView = Ti.UI.createView({
height:'55',
top:5,
bottom:5,
left:5,
right:5,
});
var avatarImageView = Ti.UI.createImageView({
image:avatar,
left:0,
top:0,
height:48,
width:48
});
postView.add(avatarImageView);
var userLabel = Ti.UI.createLabel({
text:user,
left:54,
width:120,
top:-48,
bottom:2,
height:16,
textAlign:'left',
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:14,
fontWeight:'bold'}
});
postView.add(userLabel);
var dateLabel = Ti.UI.createLabel({
text:created_at,
right:0,
top:-18,
bottom:2,
height:14,
textAlign:'right',
width:110,
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:12}
});
postView.add(dateLabel);
var tweetTextLabel = Ti.UI.createLabel({
text:tweetText,
left:54,
top:0,
bottom:2,
height:'auto',
width:236,
textAlign:'left',
font:{fontSize:14}
});
postView.add(tweetTextLabel);
row.add(postView);
twitterData[i] = row;
}
var tableview = Titanium.UI.createTableView({data:twitterData,
minRowHeight:58});
win1.add(tableview);
}
catch(E) {
alert(E);
}
};
httpClient.send();
Please keep in mind .. We need to use either top and left or bottom and right.. dont mix up top and bottom in same element....
coming to solution to yours .. give top to table so that it goes down .......
i have created top view where you can specify username and etc
below you can post timeline
var win1=Ti.UI.createWindow()
///// this is top portion
var topView=Ti.UI.createView({
top:0,
left:0,
height:100,
background:"#2d2d2d"
})
var username=Ti.UI.createLabel({
text:"foundationsix",
color:"#fff"
})
topView.add(username)
win1.add(topView)
var twitterUserName = "foundationsix";
var httpClient = Ti.Network.createHTTPClient();
httpClient.timeout = 10000;
httpClient.open("GET","http://api.twitter.com/1/statuses/" + "user_timeline.json?count=10&screen_name=" + twitterUserName);
var twitterData = [];
httpClient.onload = function() {
try {
var tweets = JSON.parse(this.responseText);
for (var i=0; i < tweets.length; i++) {
var tweetText = tweets[i].text;
var user = tweets[i].user.screen_name;
var avatar = tweets[i].user.profile_image_url;
var created_at = tweets[i].created_at;
var row = Ti.UI.createTableViewRow({hasChild:true,
height:'auto'});
var postView = Ti.UI.createView({height:55});
var avatarImageView = Ti.UI.createImageView({
image:avatar,
left:0,
top:0,
height:48,
width:48
});
postView.add(avatarImageView);
var userLabel = Ti.UI.createLabel({
text:user,
left:54,
width:120,
top:-48,
bottom:2,
height:16,
textAlign:'left',
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:14,
fontWeight:'bold'}
});
postView.add(userLabel);
var dateLabel = Ti.UI.createLabel({
text:created_at,
right:0,
top:-18,
bottom:2,
height:14,
textAlign:'right',
width:110,
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:12}
});
postView.add(dateLabel);
var tweetTextLabel = Ti.UI.createLabel({
text:tweetText,
left:54,
top:0,
bottom:2,
height:'auto',
width:236,
textAlign:'left',
font:{fontSize:14}
});
postView.add(tweetTextLabel);
row.add(postView);
twitterData[i] = row;
}
var tableview = Titanium.UI.createTableView({
data:twitterData,
minRowHeight:58,
top:100
});
win1.add(tableview);
}
catch(E) {
alert(E);
}
};
httpClient.send();
win1.open()

HTML5 geolocation iphone

Heey People,
I've wrote something to find the gps location of my iphone.
If my location is found it should show my location in an alertbox.
But apparently it keeps showing me the alertbox on my iphone over and over again.
I've wrote my class so that I can set an callback function.
Because when you try to get your location it is not set till your found.
The callback function will be executed when your location is found.
Can anyone tell me how to stop the location tracker after I'm found?
My class:
GPS = function() {
this.constructor();
}
var gpsLatitude = 0.0, gpsLongitude = 0.0, gpsCallback;
GPS.prototype.constructor = function(){
this.getLocation();
}
var afterNotification = function(){
//Do something
};
GPS.prototype.setCallback = function(myfunction){
gpsCallback = myfunction;
}
GPS.prototype.getLocation = function(){
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(this.showPosition, this.showError);
}
else{
Lungo.Notification.error(
"Error", //Title
"Your device doesnt support GPS", //Description
"cancel", //Icon
7, //Time on screen
afterNotification //Callback function
);
}
}
GPS.prototype.showPosition = function(position)
{
gpsLatitude = position.coords.latitude;
gpsLongitude = position.coords.longitude;
gpsCallback();
}
GPS.prototype.getLatitude = function()
{
return gpsLatitude;
}
GPS.prototype.getLongitude = function()
{
return gpsLongitude;
}
GPS.prototype.showError = function(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
Lungo.Notification.error(
"Error", //Title
"Gebruiker staat geolocatie niet toe", //Description
"cancel", //Icon
7, //Time on screen
afterNotification //Callback function
);
break;
case error.POSITION_UNAVAILABLE:
Lungo.Notification.error(
"Error", //Title
"Locatie niet beschikbaar", //Description
"cancel", //Icon
7, //Time on screen
afterNotification //Callback function
);
break;
case error.TIMEOUT:
Lungo.Notification.error(
"Error", //Title
"Locatie timeout", //Description
"cancel", //Icon
7, //Time on screen
afterNotification //Callback function
);
break;
case error.UNKNOWN_ERROR:
Lungo.Notification.error(
"Error", //Title
"Unkown location error", //Description
"cancel", //Icon
7, //Time on screen
afterNotification //Callback function
);
break;
}
}
var GPS = new GPS();
The way I use the class:
var callback = function()
{
alert(GPS.getLongitude() + ", " + GPS.getLatitude());
}
GPS.setCallback(callback);
I,ve found my problem, I requested my location again in the getLattitude method. After removing this is works perfect.

Response is not working for the code in button action listener

var button = Ti.UI.createButton({
left: 180,
top: 180,
width: 100,
hight: 30,
title: 'Go'
});
self.add(button);
button.addEventListener('click', function() {
var xhr = Ti.Network.createHTTPClient();
var authstr = 'Basic ' +Titanium.Utils.base64encode('S0009231839'+':'+ 'm8390967743!');
xhr.open("GET","http://erp.esworkplace.sap.com/sap/bc/srt/wsdl/bndg_DF52FF9E9AF025F18F0400145E5ADE89/wsdl11/allinone/ws_policy/document?sap-client=800");
xhr.setRequestHeader('Authorization', authstr);
xhr.setRequestHeader('Content-Type','text/xml');
xhr.setRequestHeader('Accept-Language','en');
alert('show');
xhr.onload = function(e)
{ try
{
//get the xml data and let it roll!
var doc = this.responseXML.documentElement;
// var items = doc.getElementsByTagName("atom:entry");
var items = doc.getElementsByTagName("wsdl:portType");
for(var c=0; c<items.length;c++){
var item = items.item(c);
alert(items.length);
var attributeEmployeeLeave = items.item(c).attributes.getNamedItem("name").nodeValue;
Ti.API.info(items.item(c).attributes.getNamedItem("name").nodeValue);
}
}
catch(E)
{
alert('error on xhr');
}
The response for the button addEventListener is not working
I dont know why!
Any one can advice me!
Simple Button Example
var button = Titanium.UI.createButton({ title: 'Hello' });
button.addEventListener('click',function(e) {
Titanium.API.info("You clicked the button"); });
Please change your button.addEventListener('click', function() { to button.addEventListener('click', function(e) {.