Im having a bit of trouble setting my text in my UIPickerView. I can get it to function and change the colour and position the text to the centre of the UIPicker but, i can't figure out how to display my Picker text. It only display this:
label.text = [NSString stringWithFormat:@"TEST"]; and not my "array" contents. I know this might be hard to understand, sorry. I have created a
@Property,
IBOutlet, and
@synthesize and did the xib connections but not luck. Maybe my code is not in the right area. Below is my code.
.H
Code:
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h> //
@interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arraySound;
IBOutlet UIButton *myButton; //
IBOutlet UIImageView *animation; //
IBOutlet UIImageView *animation2; //
IBOutlet UILabel *label; //FONT
IBOutlet UILabel *label2; //FONT 2
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView; //
@property (nonatomic, retain) IBOutlet UIButton *myButton; //
@property (nonatomic, retain) IBOutlet UIPickerView *label3; // picker font
-(IBAction) myAction; //
-(IBAction) myAction2; //
// ALL SOUNDS NEED TO BE LABELED HERE ---------------------- START OF LABELING --------------------
-(void)mySound1; //
// ALL SOUNDS NEED TO BE LABELED HERE ---------------------- END OF LABELING --------------------
@end
.M
Code:
#import "PickerViewController.h"
@implementation PickerViewController
@synthesize myButton, pickerView, label3; //
int myInt = 0;
//LABEL ALL SOUDS HERE --------------------------- START -------------
-(void)mySound1 //sound1
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Fart138" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);
}
//LABEL ALL SOUNDS HERE --------------------------- END -------------
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
// CUSTOM FART START
[label setFont:[UIFont fontWithName:@"pastel" size:64]];
[label2 setFont:[UIFont fontWithName:@"Chalkduster" size:32]];
// CUSTOM FART END
[super viewDidLoad];
//LABEL ALL SOUND ARRYAS HERE --------------------------- START -------------
arraySound = [[NSMutableArray alloc] init];
[arraySound addObject:@"Red"]; //sound1
[arraySound addObject:@"Orange"]; //sound2
[arraySound addObject:@"Yellow"]; //sound3
[arraySound addObject:@"Green"]; //sound4
[arraySound addObject:@"Blue"]; //sound5
//LABEL ALL SOUND ARRAYS HERE --------------------------- START -------------
[pickerView selectRow:0 inComponent:0 animated:NO];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//START OF TEXT CENTER PICKER -----------------------
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 14)];
label.text = [NSString stringWithFormat:@"TEST"];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
//END OF TEXT CENTER PICKER ------------------------
//LABEL ALL SOUND ROW HERE --------------------------- START -------------
if (row == 0) { myInt =0; } //sound1
if (row == 1) { myInt =1; } //sound2
if (row == 2) { myInt =2; } //sound3
if (row == 3) { myInt =3; } //sound4
if (row == 4) { myInt =4; } //sound5
//LABEL ALL SOUND ROW HERE --------------------------- END -------------
NSLog(@"Selected Sound: %@. Index of selected Sound: %i", [arraySound objectAtIndex:row], row);
}
-(IBAction) myAction
{
if(myInt == 0) { [self mySound1]; }
{ //START OF ANIMATION
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"cloud1.png"],
[UIImage imageNamed:@"cloud2.png"],
[UIImage imageNamed:@"cloud3.png"],
[UIImage imageNamed:@"cloud4.png"],
[UIImage imageNamed:@"cloud5.png"],
[UIImage imageNamed:@"cloud6.png"],
[UIImage imageNamed:@"cloud7.png"],
nil];
[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];
} // END OF ANIMATION
}
-(IBAction) myAction2
{ //START OF ANIMATION
animation2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"tushypoker2backup.png"],
[UIImage imageNamed:@"tushypokerbackup.png"],
nil];
[animation2 setAnimationRepeatCount:1];
animation2.animationDuration = .2;
[animation2 startAnimating];
} // END OF ANIMATION
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arraySound release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arraySound count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arraySound objectAtIndex:row];
}
@end
Ask me any questions. Thanks for any kind of help!