Buttons and textboxes not showing text - microsoft-band

I'm trying to create a tile with 3 pages, each page contain a textbox and a button. Here is my code
[self.client.personalizationManager
themeWithCompletionHandler:^(MSBTheme *theme, NSError *error){
if (error){
// handle error
NSLog(#"Error: %#", error);
}
NSLog(#"Creating tile...");
NSString *tileName = #"SnapBand";
// Create Tile Icon
MSBIcon *tileIcon = [MSBIcon iconWithUIImage:[UIImage imageNamed:#"Band46x46.png"] error:nil];
// Create small Icon
MSBIcon *smallIcon = [MSBIcon iconWithUIImage:[UIImage imageNamed:#"Band24x24.png"] error:nil];
// Create a Tile
NSUUID *tileID = [[NSUUID alloc] initWithUUIDString:#"6120f0c7-fec4-44b0-99fa-04c7ca6cf869"];
MSBTile *tile = [MSBTile tileWithId:tileID name:tileName tileIcon:tileIcon smallIcon:smallIcon error:nil];
// Create a Picture Text Block
MSBPageTextBlock *pictureTextBlock = [self createMSBandTextBlockWithParameters:10 WithTheme:theme];
// Create a Picture Snap Button
MSBPageTextButton *pictureButton = [self createMSBandActioButtonWithParameters:11 WithTheme:theme];
// Create a Movie Text Block
MSBPageTextBlock *movieTextBlock = [self createMSBandTextBlockWithParameters:20 WithTheme:theme];
// Create a Movie Snap Button
MSBPageTextButton *movieButton = [self createMSBandActioButtonWithParameters:21 WithTheme:theme];
// Create a Brust Text Block
MSBPageTextBlock *brustTextBlock = [self createMSBandTextBlockWithParameters:30 WithTheme:theme];
// Create a Brust Snap Button
MSBPageTextButton *brustButton = [self createMSBandActioButtonWithParameters:31 WithTheme:theme];
MSBPageFlowPanel *pictureFlowPanel = [[MSBPageFlowPanel alloc] initWithRect:[MSBPageRect rectWithX:0 y:0 width:245 height:105]];
MSBPageFlowPanel *movieFlowPanel = [[MSBPageFlowPanel alloc] initWithRect:[MSBPageRect rectWithX:245 y:0 width:245 height:105]];
MSBPageFlowPanel *brustFlowPanel = [[MSBPageFlowPanel alloc] initWithRect:[MSBPageRect rectWithX:490 y:0 width:245 height:105]];
[pictureFlowPanel addElements:#[pictureTextBlock, pictureButton]];
[movieFlowPanel addElements:#[movieTextBlock, movieButton]];
[brustFlowPanel addElements:#[brustTextBlock, brustButton]];
MSBPageScrollFlowPanel *panel = [[MSBPageScrollFlowPanel alloc] initWithRect:[MSBPageRect rectWithX:0 y:0 width:245 height:105]];
panel.horizontalAlignment = MSBPageHorizontalAlignmentLeft;
panel.verticalAlignment = MSBPageVerticalAlignmentTop;
panel.orientation = MSBPageFlowPanelOrientationHorizontal;
[panel addElements:#[pictureFlowPanel, movieFlowPanel, brustButton]];
MSBPageLayout *layout = [[MSBPageLayout alloc] init];
layout.root = panel;
[tile.pageLayouts addObject:layout];
[self.client.tileManager addTile:tile completionHandler:^(NSError *error) {
if (!error || error.code == MSBErrorTypeTileAlreadyExist)
{
NSLog(#"Creating page...");
NSUUID *pageID = [[NSUUID alloc] initWithUUIDString:#"a234e9ba-1c09-46c3-b3b3-12bb1c9cf90f"];
NSArray *pageValues = #[[MSBPageTextButtonData pageTextButtonDataWithElementId:11 text:#"Snap!" error:nil],
[MSBPageTextBlockData pageTextBlockDataWithElementId:10 text:#"Picture" error:nil]];
MSBPageData *pageDataPicture = [MSBPageData pageDataWithId:pageID layoutIndex:0 value:pageValues];
NSUUID *pageIDMovie = [[NSUUID alloc] initWithUUIDString:#"1535d78b-9784-4892-a94c-030df63de453"];
NSArray *pageValuesMovie = #[[MSBPageTextButtonData pageTextButtonDataWithElementId:21 text:#"Snap!" error:nil],
[MSBPageTextBlockData pageTextBlockDataWithElementId:20 text:#"Movie" error:nil]];
MSBPageData *pageDataMovie = [MSBPageData pageDataWithId:pageIDMovie layoutIndex:1 value:pageValuesMovie];
NSUUID *pageIDBrust = [[NSUUID alloc] initWithUUIDString:#"1fd74789-3936-4324-a1fc-3e1d66d6e189"];
NSArray *pageValuesBrust = #[[MSBPageTextButtonData pageTextButtonDataWithElementId:31 text:#"Snap!" error:nil],
[MSBPageTextBlockData pageTextBlockDataWithElementId:30 text:#"Brust" error:nil]];
MSBPageData *pageDataBrust = [MSBPageData pageDataWithId:pageIDBrust layoutIndex:2 value:pageValuesBrust];
[self.client.tileManager setPages:#[pageDataPicture, pageDataMovie, pageDataBrust] tileId:tile.tileId completionHandler:^(NSError *error) {
if (!error)
{
NSLog(#"Completed band tile creation");
[self.snapPictureButton setBackgroundImage:[UIImage imageNamed:#"GreenStatus"] forState:UIControlStateNormal];
[self sendHapticFeedback];
}
else
{
NSLog(#"Error: %#", error.localizedDescription);
}
}];
}
else
{
NSLog(#"Error: %#", error.localizedDescription);
}
}];
}];
I created a constructor method to generate textboxes and buttons, the methods are:
-(MSBPageTextBlock *) createMSBandTextBlockWithParameters:(int)textBoxID WithTheme:(MSBTheme *) theme{
MSBPageTextBlock *textBlock = [[MSBPageTextBlock alloc] initWithRect:[MSBPageRect rectWithX:0 y:0 width:200 height:40] font:MSBPageTextBlockFontSmall];
textBlock.elementId = textBoxID;
textBlock.baselineAlignment = MSBPageTextBlockBaselineAlignmentRelative;
textBlock.horizontalAlignment = MSBPageHorizontalAlignmentLeft;
textBlock.autoWidth = YES;
textBlock.color = theme.baseColor;
textBlock.margins = [MSBPageMargins marginsWithLeft:15 top:0 right:0 bottom:0];
return textBlock;
}
and
-(MSBPageTextButton *) createMSBandActioButtonWithParameters:(int)textBoxID WithTheme:(MSBTheme *) theme{
MSBPageTextButton *actionButton = [[MSBPageTextButton alloc] initWithRect:[MSBPageRect rectWithX:0 y:0 width:200 height:40]];
actionButton.elementId = textBoxID;
actionButton.horizontalAlignment = MSBPageHorizontalAlignmentCenter;
actionButton.pressedColor = theme.baseColor;
actionButton.margins = [MSBPageMargins marginsWithLeft:15 top:0 right:0 bottom:0];
return actionButton;
}
The buttons and the textboxes doesn't show any text on the band, i can see the buttons on my band, the position of the third button is not correct, and there is no text in two of the buttons.
Can anyone help me?

Looks like you have created one layout with a horizontal scroll panel. And that panel has pictureFlowPanel, movieFlowPanel, brustButton as its elements.
So when you create pages you will just need to create one page with the above layout. However, in your code I see you are trying to create three pages with layout index 0, 1 and 2. Only layout 0 is valid and layouts 1 and 2 do not exist.
One possible fix is to break apart the layouts into three separate layouts:
MSBPageLayout *pictureLayout = [[MSBPageLayout alloc] init];
pictureLayout.root = pictureFlowPanel;
[tile.pageLayouts addObject:pictureLayout]; //Layout 0
MSBPageLayout *movieLayout = [[MSBPageLayout alloc] init];
movieLayout.root = movieFlowPanel;
[tile.pageLayouts addObject:movieLayout]; //Layout 1
MSBPageLayout *burstLayout = [[MSBPageLayout alloc] init];
burstLayout.root = burstFlowPanel;
[tile.pageLayouts addObject:burstLayout]; //Layout 2

Related

Repeating Audio in WatchKit (AVAudioPlayer?)?

I am wanting to loop a local audio file in my Apple Watch App. Currently I am using AVAudioPlayerNode and AVAudioEngine which works well but I cannot figure out how to loop the sound.
I noticed that I can use AVAudioPlayer, which has the handy "numberOfLoops" but, for some reason AVAudioPlayer is not working on the watch. I have no idea why.
Here is my current code to play a sound:
_audioPlayer = [[AVAudioPlayerNode alloc] init];
_audioEngine = [[AVAudioEngine alloc] init];
[_audioEngine attachNode:_audioPlayer];
AVAudioFormat *stereoFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:2];
[_audioEngine connect:_audioPlayer to:_audioEngine.mainMixerNode format:stereoFormat];
if (!_audioEngine.isRunning) {
NSError* error;
[_audioEngine startAndReturnError:&error];
}
NSError *error;
NSBundle* appBundle = [NSBundle mainBundle];
NSURL *url = [NSURL fileURLWithPath:[appBundle pathForResource:#"FILE_NAME" ofType:#"mp3"]];
AVAudioFile *asset = [[AVAudioFile alloc] initForReading:url error:&error];
[_audioPlayer scheduleFile:asset atTime:nil completionHandler:nil];
[_audioPlayer play];
Here is the code i've tried to use for AVAudioPlayer, but does not work:
NSError *audioError;
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"FILE_NAME" ofType:#"mp3"]] error:&audioError];
player.numberOfLoops = MAXFLOAT;
player.delegate = self;
[player play];
I am using WatchKit 5.0(+).
You can loop your AVAudioFile by recursively scheduling it:
__block __weak void (^weakSheduleFile)(void);
void (^scheduleFile)(void);
weakSheduleFile = scheduleFile = ^{ [self->_audioPlayer scheduleFile:asset atTime:nil completionHandler:weakSheduleFile]; };
scheduleFile();
I'm not sure if this will be a seamless loop. If it's not, you can try always having two files scheduled:
scheduleFile();
scheduleFile();
If your audio file fits into memory, you could schedule playback as an AVAudioBuffer with the AVAudioPlayerNodeBufferLoops option (N.B. only tested on simulator!):
AVAudioFormat *outputFormat = [_audioPlayer outputFormatForBus:0];
__block AVAudioPCMBuffer *srcBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:asset.processingFormat frameCapacity:(AVAudioFrameCount)asset.length];
if (![asset readIntoBuffer:srcBuffer error:&error]) {
NSLog(#"Read error: %#", error);
abort();
}
AVAudioPCMBuffer *dstBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:outputFormat frameCapacity:(AVAudioFrameCount)asset.length];
AVAudioConverter *converter = [[AVAudioConverter alloc] initFromFormat:srcBuffer.format toFormat:dstBuffer.format];
AVAudioConverterOutputStatus status = [converter convertToBuffer:dstBuffer error:&error withInputFromBlock:^AVAudioBuffer * _Nullable(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus * _Nonnull outStatus) {
if (srcBuffer) {
AVAudioBuffer *result = srcBuffer;
srcBuffer = NULL;
*outStatus = AVAudioConverterInputStatus_HaveData;
return result;
} else {
*outStatus = AVAudioConverterInputStatus_EndOfStream;
return NULL;
}
}];
assert(status != AVAudioConverterOutputStatus_Error);
[_audioPlayer scheduleBuffer:dstBuffer atTime:nil options:AVAudioPlayerNodeBufferLoops completionHandler:nil];
[_audioPlayer play];

TableViewCell doesn't update correctly

I have this tableviewcell.m to configure my cells.
It is configuring the text and information correctly but I just can't get it to update the color according to some other information.
Here's the code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
priceLabel.textAlignment = UITextAlignmentRight;
[priceLabel setFont:[UIFont systemFontOfSize:12.0]];
if ([account.income isEqualToString:#"income"]){
[priceLabel setTextColor:[UIColor greenColor]];
} else if ([account.income isEqualToString:#"expense"]) {
[priceLabel setTextColor:[UIColor redColor]];
} //label, alignment, font are working correctly
//but the if statement doesn't get there
}
The color though, not working at all.
It seems to me that the if statement is being completely ignored.
Could anyone help me to understand why or suggest a better programming approach ?
Here's the cell for row code. I don't know if it will help because they aren't in the same file. Here I'm only referencing my tableviewcell file:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create a TableViewCell, then set its account to the account for the current row.
static NSString *AccountCellIdentifier = #"AccountCellIdentifier";
TableViewCell *accountCell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:AccountCellIdentifier];
if (accountCell == nil) {
accountCell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AccountCellIdentifier] autorelease];
accountCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[self configureCell:accountCell atIndexPath:indexPath];
return accountCell;
}
//and here the cell config
- (void)configureCell:(TableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell
Account *account = (Account *)[fetchedResultsController objectAtIndexPath:indexPath];
cell.account = account;
}
Many thanks !
Here's the code where i get the texts:
- (void)setAccount:(Account *)newAccount {
if (newAccount != account) {
[account release];
account = [newAccount retain];
}
nameLabel.text = account.name;
accountLabel.text = Account.accounttype;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *stringcost = [numberFormatter stringFromNumber:account.cost];
priceLabel.text = stringcost;
}
Yes! I knew I was missing something simple.
All I had to do is "move" that If... statement to where the implementation of the cell is setting the text. There and only there it will update the color (or anything else) so here's my final code that's working perfectly now...
- (void)setAccount:(Account *)newAccount {
if (newAccount != account) {
[account release];
account = [newAccount retain];
}
nameLabel.text = account.name;
accountLabel.text = Account.accounttype;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *stringcost = [numberFormatter stringFromNumber:account.cost];
priceLabel.text = stringcost;
if ([account.income isEqualToString:#"income"]){
[priceLabel setTextColor:[UIColor greenColor]];
} else if ([account.income isEqualToString:#"expense"]) {
[priceLabel setTextColor:[UIColor redColor]];
} //this will give me green text color for income and red text color for expense
}
Special thanks for Michael Dauterman who inspired me to stay awake for hours through the night 'till my function is actually working.

NSException error seems to occur randomly

This code was working fine, now for some reason I'm getting an NSException error that's generating a SIGABRT at the following line of code...
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:wbPopOverController] autorelease];
Here's the method it's located in...
-(IBAction)showPop:(id)sender {
//close any open popovers
[self.popoverController dismissPopoverAnimated:NO];
// Initialize wbPopUpViewController
PopoverAircraftClass *wbPopOverController = [[PopoverAircraftClass alloc] initWithNibName:#"PopoverAircraftClass" bundle:[NSBundle mainBundle]];
wbPopOverController.contentSizeForViewInPopover = CGSizeMake(175, 216);
wbPopOverController.delegate = self; //for update
[wbPopOverController setAircraftList:aircraftList withCurrentValue:activeRegistration];
// Object converted to UIPopOverController (retained so we can reference it)
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:wbPopOverController] autorelease];
self.popoverController.passthroughViews = [NSArray arrayWithObject: self.view];
[self.popoverController presentPopoverFromRect:[sender frame] inView:self.layoutViewIB permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
[wbPopOverController release];
}
I'm new at this as you can probably tell. Any ideas as to what's generating the NSException?

Help with UITableView / Navigation Setup with UITabBar Interface

I'm trying to set up a Tab Bar Application as my root controller, and one of the tabs to be a Navigation setup. On my Root View Controller of the Navigation View, I have one child level that is a subclass of the UIViewController called IntervalViewController.
I have this in my Root View Controller (IntervalSetupViewController.m):
#import "IntervalSetupViewController.h"
#import "IntervalViewController.h"
#implementation IntervalSetupViewController
#synthesize controllers; // My Array for the Table View Cells
- (void)viewDidLoad {
self.title = #"Interval Setup";
NSMutableArray *array = [[NSMutableArray alloc] init];
// Interval List Setup
IntervalViewController *newInterval = [[IntervalViewController alloc]
initWithStyle:UITableViewStylePlain];
newInterval.title = #"Interval 1";
[array addObject:newInterval];
[newInterval release];
self.controllers = array;
[array release];
[super viewDidLoad];
}
- (void)viewDidUnload {
self.controllers = nil;
[super viewDidUnload];
}
- (void)dealloc {
[controllers release];
[super dealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [self.controllers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *IntervalCell = #"IntervalCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IntervalCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:IntervalCell] autorelease];
}
// Configure the cell
NSUInteger row = [indexPath row];
IntervalViewController *newController = [controllers objectAtIndex:row];
cell.textLabel.text = newController.title;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
IntervalViewController *nextController = [self.controllers objectAtIndex:row];
[self.navigationController pushViewController:nextController animated:YES];
}
#end
This is the error screen from the console:
* -[IntervalViewController initWithStyle:]: unrecognized selector sent to instance 0x3b33e60
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[IntervalViewController initWithStyle:]: unrecognized selector sent to instance 0x3b33e60'
I assume that I am passing an invalid argument to my initWithStyle method, but I thought UITableViewStylePlain was a valid parameter. Am I setting this up right? I was following the example in APress Beginning iPhone Development 3 book and trying to modify it a little to what I wanted to do, but I'm new to coding. Thanks!
initWithStyle is used to initialize a UITableViewController not a UIViewController

How to load UIImagePickerController at viewLoad

This seems like a really simple idea... I have a button that says "load a picture". When that button is clicked I want to remove the current view, load a new one, open the image picker, get the selected picture, and display it in the view that I just loaded. I have read several documents that say UIImagePicker does not work in viewDidLoad, and found one thread that suggested using awakeFromNib instead but even that does not work... here is code. Please help.
In MenuViewController
-(IBAction)loadPictureButtonPressed
{
UIView *parent = [self.view superview];
if(self.loadPictureViewController.view.superview == nil)
{
if(self.loadPictureViewController == nil)
{
LoadPictureViewController *picController = [[LoadPictureViewController alloc]
initWithNibName:#"LoadPictureView" bundle: nil];
self.loadPictureViewController = picController;
[picController release];
}
}
[parent addSubview:self.loadPictureViewController.view];
[self.view removeFromSuperview];
}
In LoadPictureViewController
-(void)awakeFromNib
{
if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error!"
message:#"Device does not support photo library";
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Again I have tried placing this code in both viewDidLoad and awakeFromNib. Please help
I simply set an NSTimer in viewDidLoad with a timer of 0 to select the method where I load it.
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:#selector(camera) userInfo:nil repeats:NO];
Instead of NSTimer, you probably want to use the following in -viewDidLoad,
[self performSelector:#selector(camera) withObject:nil afterDelay:0];

Resources