Get reference to custom UISlider - iphone

I have implemented a custom class of UISlider so the touchable areas are larger
Everything is working great except now I want to change the initial values depending on other factors in the app.
The only code I have gets the slider's ID from (sender)
I need to address the slider like so...
slider.value = 25.0;
But the only code I have is
-(IBAction) slider1Changed:(id) sender{
UISlider *slider = (UISlider *) sender;
int progressAsInt =(int)(slider.value);
NSLog(#"Slider is %#",slider);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:progressAsInt forKey:#"SharedSkilledWorkforce"];
}
In the xib #property (nonatomic, retain) UISlider *slider1; doesn't show up as a conncetion
How do I address the custom UISlider?

Have you tried adding IBOutlet to your slider1 property?
This:
#property (nonatomic, retain) UISlider *slider1;
Needs to be:
#property (nonatomic, retain) IBOutlet UISlider *slider1;
in order to show up in IB. IBOutlet is a hint to the designer similar to IBAction.

Related

Retain UILabel value between random views

I'm making a view based quiz app for the iPhone where the player is going randomly between three different views when answering the questions. They get +1 when answering correct and -1 when tapping the wrong answer. But after answering the question, when you go to the new random view, I need the UILabel to show the score from the previous view. How do I do that?
Here is my code:
ViewController.h
#interface ViewController : UIViewController {
IBOutlet UILabel *labelQuestion;
IBOutlet UILabel *labelAnswer1;
IBOutlet UILabel *labelAnswer2;
IBOutlet UILabel *labelAnswer3;
IBOutlet UILabel *labelScore;
int score;
}
#property (nonatomic, retain) IBOutlet UILabel *labelScore;
#property (nonatomic) int score;
ViewController.m
#synthesize labelScore;
#synthesize score;
-(IBAction)CorrectAnswer; {
score = score +1;
labelScore.text = [NSString stringWithFormat:#"%i", score];
}
-(IBAction)WrongAnswer; {
score = score -1;
labelScore.text = [NSString stringWithFormat:#"%i", score];
}
Make the variable score global i.e. define this in your AppDelegate and you can access it throughout your application.
the quickest but dirty way is to store it in app delegate.. and if you want it to persist between app launches, store it in user defaults..
but as i said this is dirty..
As they're properties, when you create the next view you can set the label's text to the current view's text and the score to the current view's.

Setting the value of a UITextField in an IBAction

I'm trying to use a UITextField as an output for a simple encryption. The field starts out with a nil string (its default behavior), and I try to update it in the encryptButtonPressed: action. I know I'm getting a good NSString into ciphertext, and I'm getting no errors or warnings or anything that the method doesn't exist, but the field remains blank. Any ideas?
Here's the header:
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *affineKeyField;
IBOutlet UITextField *shiftKeyField;
IBOutlet UITextField *messageField;
IBOutlet UITextField *ciphertextField;
MAAffineMachine* machine;
}
#property (nonatomic, retain) UITextField *affineKeyField;
#property (nonatomic, retain) UITextField *shiftKeyField;
#property (nonatomic, retain) UITextField *messageField;
#property (nonatomic, retain) UITextField *ciphertextField;
#property (nonatomic, retain) UIButton *encryptButton;
#property (nonatomic, retain) UIButton *clipboardButton;
- (IBAction)encryptButtonPressed:(id)sender;
- (IBAction)clipboardButtonPressed:(id)sender;
#end
And the action, defined in the controller's implementation:
- (IBAction)encryptButtonPressed:(id)sender {
NSLog(#"Encrypt Button pushed.\n");
int affine = [[affineKeyField text] intValue];
int shift = [[shiftKeyField text] intValue];
NSString* message = [messageField text];
NSLog(#"%d, %d, %#", affine, shift, message);
NSString* ciphertext = [machine encryptedStringFromString:message
ForAffine:affine
Shift:shift];
NSLog(#"%#\n", ciphertext);
[[self ciphertextField] setText: ciphertext];
}
If ciphertext has a proper value, you should check that ciphertextField is linked properly in the interface builder.
Try [[self ciphertextField] setText: ciphertext] -> [ciphertextField setText: ciphertext] or cipherTextField.text = ciphertext;

how to generate an event

I created a subclass from UIView
#import <UIKit/UIKit.h>
#interface MeeSelectDropDownView : UIView {
UILabel *mainText;
UIImage *bgImg;
UIImageView *bgView;
UIImageView *originView;
NSMutableArray *labelArray;
int selectedItem;
BOOL inSelectTag;
float _defaultHeight;
}
#property (nonatomic , retain) UIImage *bgImg;
#property (nonatomic , retain) UIImageView *bgView;
#property (nonatomic , retain) NSMutableArray *labelArray;
#property (nonatomic , retain) UIImageView *originView;
#property (nonatomic , retain) UILabel *mainText;
#property (nonatomic , readonly) int selectedItem;
- (void) setViewHeight:(float)aheight;
-(void) showDropList;
-(void) hiddenDropList;
-(void) setStringByArray:(NSArray*)array;
-(void)hiddenLabels
{
for(UILabel *aLabel in labelArray){
[aLabel removeFromSuperview];
}
}
Is it possible to generate an Event from function 'hiddenLabels' to inform and do somethings
Thanks
interdev
It is totally unclear what you are trying to do.
Suggestion: you can consider either posting an NSNotification, or registering observers using KVO (Key Value Observing).

Play a sound based on the button pressed on the IPhone/xcode

I'm trying to play a sound based on which button is pressed using AVAudioPlayer.
(This is not a soundboard or fart app.)
I have linked all buttons using this code in the header file:
#interface appViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player;
UIButton *C4;
UIButton *Bb4;
UIButton *B4;
UIButton *A4;
UIButton *Ab4;
UIButton *As4;
UIButton *G3;
UIButton *Gb3;
UIButton *Gs3;
UIButton *F3;
UIButton *Fs3;
UIButton *E3;
UIButton *Eb3;
UIButton *D3;
UIButton *Db3;
UIButton *Ds3;
UIButton *C3;
UIButton *Cs3;
}
#property (nonatomic, retain) AVAudioPlayer *player;
#property (nonatomic, retain) IBOutlet UIButton *C4;
#property (nonatomic, retain) IBOutlet UIButton *B4;
#property (nonatomic, retain) IBOutlet UIButton *Bb4;
#property (nonatomic, retain) IBOutlet UIButton *A4;
#property (nonatomic, retain) IBOutlet UIButton *Ab4;
#property (nonatomic, retain) IBOutlet UIButton *As4;
#property (nonatomic, retain) IBOutlet UIButton *G3;
#property (nonatomic, retain) IBOutlet UIButton *Gb3;
#property (nonatomic, retain) IBOutlet UIButton *Gs3;
#property (nonatomic, retain) IBOutlet UIButton *F3;
#property (nonatomic, retain) IBOutlet UIButton *Fs3;
#property (nonatomic, retain) IBOutlet UIButton *E3;
#property (nonatomic, retain) IBOutlet UIButton *Eb3;
#property (nonatomic, retain) IBOutlet UIButton *D3;
#property (nonatomic, retain) IBOutlet UIButton *Db3;
#property (nonatomic, retain) IBOutlet UIButton *Ds3;
#property (nonatomic, retain) IBOutlet UIButton *C3;
#property (nonatomic, retain) IBOutlet UIButton *Cs3;
- (IBAction) playNote;
#end
Buttons are all linked to the event "playNote" in interfaceBuilder and each note is linked to the proper referencing outlet according to note name.
All *.mp3 sound files are named after the UIButton name (IE- C3 == C3.mp3).
In my implementation file, I have this to play a only one note when the C3 button is pressed:
#import "sonicfitViewController.h"
#implementation appViewController
#synthesize C3, Cs3, D3, Ds3, Db3, E3, Eb3, F3, Fs3, G3, Gs3, A4, Ab4, As4, B4, Bb4, C4;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"3C" ofType:#"mp3"];
NSLog(#"path: %#", path);
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
AVAudioPlayer *p = [[AVAudioPlayer alloc]
initWithContentsOfURL:file error:nil];
[file release];
self.player = p;
[p release];
[player prepareToPlay];
[player setDelegate:self];
[super viewDidLoad];
}
- (IBAction) playNote {
[self.player play];
}
Now, with the above I have two issues:
First, the NSLog reports NULL and crashes when trying to play the file. I have added the mp3's to the resources folder and they have been copied and not just linked. They are not in an subfolder under the resources folder.
Secondly, how can I set it up so that when say button C3 is pressed, it plays C3.mp3 and F3 plays F3.mp3 without writing duplicate lines of code for each different button? playNote should be like
NSString *path = [[NSBundle mainBundle] pathForResource:nameOfButton ofType:#"mp3"];
instead of defining it specifically (#"C3").
Is there a better way of doing this and why does the *path report NULL and crash when I load the app?
I'm pretty sure it's something as simple as adding additional variable inputs to - (IBAction) playNote:buttonName and putting all the code to call AVAudioPlayer in the playNote function but I'm unsure of the code to do this.
Performing multiple actions with same selector is a common task. Just set a tag property for each button and use a NSArray of NSStrings to store file names. Your even need not create each button separately. See code sample for similar question problem with selector in iphone? answer.
Check if your files is realy copied to bundle. For example in simulator
log your bundle directory with NSLog(#"myBundle:%#",[[NSBundle mainBundle] bundlePath]);
and look at files.

How can I store UIButtons in an array?

I have a method tied to four buttons. I want to create an array containing each button, and later retrieve and interact w/ a button from the array. The code I was tinkering with below. When I try to get a button from the array and send it a message, it goes kablooie.
Any thoughts on what I'm doing wrong?
Hack_DumpViewController.h
#import <UIKit/UIKit.h>
#interface Hack_DumpViewController : UIViewController {
IBOutlet UIButton *redButton;
IBOutlet UIButton *greenButton;
IBOutlet UIButton *blueButton;
IBOutlet UIButton *yellowButton;
NSArray *buttonMapping;
}
- (IBAction) changeToYo:(id)sender;
#property (nonatomic, retain) UIButton *redButton;
#property (nonatomic, retain) UIButton *greenButton;
#property (nonatomic, retain) UIButton *blueButton;
#property (nonatomic, retain) UIButton *yellowButton;
#property (nonatomic, retain) NSArray *buttonMapping;
#end
Hack_DumpViewController.m
#import "Hack_DumpViewController.h"
#implementation Hack_DumpViewController
#synthesize redButton;
#synthesize greenButton;
#synthesize yellowButton;
#synthesize blueButton;
#synthesize buttonMapping;
- (IBAction) changeToYo:(id)sender {
NSLog(#"changing numbers!");
for (UIButton *b in buttonMapping) {
[b setTitle:#"yo!"];
}
NSLog(#"changed to numbers!");
}
- (void)viewDidLoad {
buttonMapping = [[NSArray alloc] initWithObjects:greenButton, redButton, yellowButton, blueButton, nil];
}
[NSArray arrayWithObjects:...] returns an autoreleased array, so by the time you use it, it no longer exists and you end up messaging an invalid pointer. What you want is [[NSArray alloc] initWithObjects:...] (remembering to release it in your dealloc).
Why not tag the views in interface builder and then treat them like an array, much easier